Search code examples
sharepointmysite

Deactivating site collection feature on MySite


We have a custom master page that is deployed to the MySite web application per these instructions - http://www.sharepointblog.com/2008/07/sp2007-custom-master-pages-on-subsites.html

However, we require the ability to deactivate the feature on all the site collections that are within the MySite webapplication. The feature is built as a site collection scope. How would we deactivate them on an application that has potentially 3000+ MySites?


Solution

  • I would build a simple console application which iterates over all site collections (MySites) of your web application and deactivates the feature. You'll have to run this piece of code with elevate privileges (SPSecurity.RunWithElevatedPrivileges) so you have the permission to deactivate a site collection feature.

    private static void DeleteWebsiteCollections()
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://MySiteHostWebApp"));
                    SPSiteCollection mySites = webApp.Sites;
    
                    foreach (SPSite site in mySites)
                    {
                        site.Features.Remove(new Guid("place your feature id here"));
    
                        if (null != site)
                        {
                            site.Dispose();
                        }
                    }
                });
            }