gpt4 book ai didi

c# - 在功能停用时删除自定义母版页错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:19:15 25 4
gpt4 key购买 nike

我有一项功能可以在激活时将自定义母版页部署到网站集中的所有网站,并希望在停用时删除自定义母版页的所有痕迹。停用时,将站点的母版页设置回 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/");*/
}
}
}
}

最佳答案

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

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

关于c# - 在功能停用时删除自定义母版页错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11560940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com