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/");*/
            }
        }
    }
}
share|improve this question

1 Answer

up vote 0 down vote accepted

You may need to get a new reference to the SPWeb and use this to call SPWeb.GetFile().

The SPWeb from the using block may still have a reference to custom.master and will need to be refreshed.

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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