2

I have a feature that deploys custom master pages to all sites in a site collection on activation, and want to delete all traces of the custom master pages on deactivation. On deactivation, after setting the site's masterpage back to v4.master, an error (Cannot remove file "custom.master". Error Code: 158.) occurs when trying the delete the custom master page that was previously set as the default. The feature doesn't finish deactivating after the error, but most of the files are deleted and the branding is already set back to v4.master. When trying to deactivate the feature again, it removes the final file custom.master without error.

I don't understand what's missing. Why does FeatureDeactivating() have to finish before custom.master can be deleted?

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using (SPSite sitecollection = (SPSite)properties.Feature.Parent)
    {
        using (SPWeb web = sitecollection.RootWeb)
        {
            string WebAppRelativePath = sitecollection.ServerRelativeUrl;
            if (!WebAppRelativePath.EndsWith("/"))
            {
                WebAppRelativePath += "/";
            }

            foreach (SPWeb site in sitecollection.AllWebs)
            {
                site.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/custom.master";
                site.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/custom.master";
                site.UIVersion = 4;
                site.Update();
            }
        }
    }
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    using (SPSite sitecollection = (SPSite)properties.Feature.Parent)
    {
        using (SPWeb web = sitecollection.RootWeb)
        {
            string WebAppRelativePath = sitecollection.ServerRelativeUrl;
            if (!WebAppRelativePath.EndsWith("/"))
            {
                WebAppRelativePath += "/";
            }

            foreach (SPWeb site in sitecollection.AllWebs)
            {
                site.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/v4.master";
                site.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/v4.master";
                site.UIVersion = 4;
                site.Update();

                WebAppRelativePath = site.Url;
                if (!WebAppRelativePath.EndsWith("/"))
                {
                    WebAppRelativePath += "/";
                }
                SPFolder folder = web.GetFolder(site.Url + "_catalogs/masterpage/images/");
                if (folder.Exists)
                    folder.Delete();
                folder.Update();

                SPFile file = web.GetFile(site.Url + "_catalogs/masterpage/custom.css");
                if(file.Exists)
                    file.Delete();
                file.Update();

                file = web.GetFile(WebAppRelativePath + "_catalogs/masterpage/html5.master");
                if(file.Exists)
                    file.Delete();
                file.Update();

                file = web.GetFile(WebAppRelativePath + "_catalogs/masterpage/custom.master");
                if (file.Exists)
                {
                    file.Delete();  // ERROR HAPPENS HERE
                }
                file.Update();

                /*file = web.GetFile(WebAppRelativePath + "_catalogs/masterpage/minimal.master");
                if(file.Exists)
                    file.Delete();
                file = web.GetFile("/_layouts/minimal.master");
                if(file.Exists)
                    file.CopyTo(WebAppRelativePath + "_catalogs/masterpage/");

                file = web.GetFile(WebAppRelativePath + "_catalogs/masterpage/default.master");
                if(file.Exists)
                    file.Delete();
                file = web.GetFile("/_layouts/default.master");
                if(file.Exists)
                    file.CopyTo(WebAppRelativePath + "_catalogs/masterpage/");*/
            }
        }
    }
}

2 Answers 2

1

The reason the code is throwing an exception is because you are trying to delete the master page before all references to it have been removed. You remove the reference from the first web and then attempt to delete the file before all references to it have been removed.

Also, you're trying to delete the master page once per web, as it is in the iteration of the foreach (SPWeb site in sitecollection.AllWebs) block. It only exists once per SPSite, not once per SPWeb, so this is unnecessary.

Have you tried moving that portion of the code outside of the foreach block so it only executes once at the end?

3
  • FeatureDeactivated() doesn't exit: Error 1 'com.company.Branding.Features.project.projectEventReceiver.FeatureDeactivated(Microsoft.SharePoint.SPFeatureReceiverProperties)': no suitable method found to override c:\Users\Administrator\Documents\Visual Studio 2010\Projects\...
    – detailCode
    Commented Jul 19, 2012 at 14:08
  • Yep, you're right... let me edit. Commented Jul 19, 2012 at 14:29
  • I figured out the answer, but can't post it for 5 more hours since I'm a noob. :P
    – detailCode
    Commented Jul 19, 2012 at 14:40
1
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        using (SPSite sitecollection = (SPSite)properties.Feature.Parent)
        {
            using (SPWeb web = sitecollection.RootWeb)
            {
                string WebAppRelativePath = sitecollection.ServerRelativeUrl;
                if (!WebAppRelativePath.EndsWith("/"))
                {
                    WebAppRelativePath += "/";
                }

                foreach (SPWeb site in sitecollection.AllWebs)
                {
                    site.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/v4.master";
                    site.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/v4.master";
                    site.UIVersion = 4;
                    site.Update();
                }
                foreach (SPWeb site in sitecollection.AllWebs)
                {
                    WebAppRelativePath = site.Url;
                    if (!WebAppRelativePath.EndsWith("/"))
                    {
                        WebAppRelativePath += "/";
                    }

                    SPFolder folder = site.GetFolder(WebAppRelativePath + "_catalogs/masterpage/images/");
                    if (folder.Exists)
                        folder.Delete();
                    folder.Update();

                    SPFile file = site.GetFile(WebAppRelativePath + "_catalogs/masterpage/custom.css");
                    if (file.Exists)
                        file.Delete();
                    file.Update();

                    file = site.GetFile(WebAppRelativePath + "_catalogs/masterpage/html5.master");
                    if(file.Exists)
                        file.Delete();
                    file.Update();

                    file = site.GetFile(WebAppRelativePath + "_catalogs/masterpage/custom.master");
                    if (file.Exists)
                    {
                        file.Delete();
                    }
                    file.Update();



                }
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.