Site Collection Publishing Infrastructure And Term Store – How to delete leftover Term Store Groups

If you have worked with the term store and site collections with the publishing infrastructure turned on in SharePoint 2010, you may have run into the issue of term store groups not being deleted. The code snippet below will take care of that for you but adding an event receiver to the site collection that will remove any associated automatically generated term store groups.

Here’s the scenario. You have created a new site collection, turned on publishing infrastructure but for some reason or another you need to delete it. So you delete the site collection, add it back again, (and this is the key part) with the same name, and turn on the publishing infrastructure again.

The users then say they  want to make use of the managed metadata term store but only allow each site collection to see its own group of term sets. No problem, you tell them. SharePoint creates a term store group for every site automatically when the publishing infrastructure is turned on. Everyone is happy until you get back to your desk and realize the term store group isn’t there.

Doing some research you find out that when you delete a site that had the publishing infrastructure feature activated SharePoint does not delete the term store group that got automatically created. So if you try to create another site with the same name, you won’t get your term store group created by the infrastructure. This is because another group in the term store has the same name. These groups are named using the site collection name. So the old term store group is still there as it was named after a site collection that had the same name as your current one.

Worse yet, you won’t be able to find this group in central admin. It’s only available to the site collection that it was paired with. That pairing is done based on the site GUID and not the site name. So when you delete the site, the connection is broken and the term store group is lost forever. The connection will not be reestablished when you create a new site collection with the same name.

To combat this, I created a little event receiver that will go into the managed metadata service and delete any groups that are named with the site collection name and the pattern used by the publishing infrastructure when it creates these groups.

Hope this helps someone one day. Enjoy.

  public override void SiteDeleting(SPWebEventProperties properties)

       {

           base.SiteDeleting(properties);

           SPSecurity.RunWithElevatedPrivileges(delegate()

           {

               SPSite site = properties.Web.Site as SPSite;

               TaxonomySession session = new TaxonomySession(site);

               string defaultName = “Site Collection – ” + (site.Url.Replace(“http://”, “”)).Replace(“https://”, “”).Replace(“/”, “-“).Replace(“:”, “-“);

               if (session.TermStores.Count > 0)

               {

                   TermStore store = session.TermStores[“Managed Metadata Service”];

                   foreach (Group thegroups in store.Groups)

                   {

                       if (thegroups.Name.ToString() == defaultName)

                       {

                           foreach (TermSet termsets in thegroups.TermSets)

                           {

                               termsets.Delete();

                           }

                           thegroups.Delete();

                       }

                   }

                   store.CommitAll();

               }

           });

       }

Comments are closed.