在功能取消激活时删除自定义母版页错误

时间:2012-07-19 12:29:51

标签: c# sharepoint sharepoint-2010 master-pages

我有一项功能,可以在激活时将自定义母版页部署到网站集中的所有网站,并希望在停用时删除所有自定义母版页的跟踪。在停用时,在将站点的母版页设置回v4.master后,在尝试删除先前设置为默认的自定义母版页时会出现错误(无法删除文件“custom.master”。错误代码:158。)。错误后该功能未完成停用,但大多数文件已被删除,并且品牌已经设置回v4.master。尝试再次停用该功能时,它会删除最终文件custom.master而不会出错。

我不明白缺少什么。为什么在删除custom.master之前必须完成FeatureDeactivating()?

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/");*/
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可能需要获得SPWeb的新引用,并使用此引用SPWeb.GetFile()

SPWeb块中的using可能仍然引用custom.master,需要刷新。