2

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

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

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

1 回答 1

2

您可能需要获取对 的新引用SPWeb并使用它来调用SPWeb.GetFile().

SPWebfrom块可能仍然有对 custom.master的using引用,并且需要刷新。

于 2012-07-19T13:18:32.397 回答