C# 功能停用时删除自定义母版页错误
我有一个功能,在激活时将自定义母版页部署到网站集中的所有站点,并希望在停用时删除自定义母版页的所有跟踪。停用时,将站点母版页设置回v4.master后,尝试删除以前设置为默认值的自定义母版页时发生错误(无法删除文件“custom.master”。错误代码:158)。该功能在出现错误后未完成停用,但大多数文件已被删除,并且品牌已设置回v4.master。再次尝试停用该功能时,会删除最终文件custom.master,不会出错 我不明白少了什么。为什么必须先完成FeatureDeactivating()才能删除custom.masterC# 功能停用时删除自定义母版页错误,c#,sharepoint,sharepoint-2010,master-pages,C#,Sharepoint,Sharepoint 2010,Master Pages,我有一个功能,在激活时将自定义母版页部署到网站集中的所有站点,并希望在停用时删除自定义母版页的所有跟踪。停用时,将站点母版页设置回v4.master后,尝试删除以前设置为默认值的自定义母版页时发生错误(无法删除文件“custom.master”。错误代码:158)。该功能在出现错误后未完成停用,但大多数文件已被删除,并且品牌已设置回v4.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/");*/
}
}
}
}
您可能需要获取对
SPWeb
的新引用,并使用该引用调用SPWeb.GetFile()
using
块中的SPWeb
可能仍有对custom.master的引用,需要刷新。不要从功能的属性中处理SPSite对象。也不要从SPSite.RootWeb处理SPWeb。仅来自SPSite.OpenWeb()。正在尝试执行与OP相同的操作。。。这是否意味着在SPWeb
使用
块之外,我使用块为新的SPWeb
创建另一个?或者我应该在第一个块中使用执行此操作?如果我能弄明白或者你能帮我,我会再给你一次投票,使用
块为新的SPWeb
对象创建一个新的。如果你想得到更详细的答案,或者它仍然不起作用,请创建一个新问题并发布一些代码,这样你会更加关注你的问题。谢谢-我最终在另一个模块中完成了这项工作(仅在卸载该功能时删除资产,而不是在停用该功能时删除资产)。我的问题是,新的using
块是在现有的using
块内部,还是在现有的using
块之后?可能会把它放在您的答案中以明确(?)我会把它放在使用
块的之外,因为这样可以更容易地阅读代码并查看发生了什么,而且它还可以确保您不会意外地使用原始的SPWeb
,而不是新创建的。这实际上可能没有什么区别,但我还没有测试过,最好还是稳妥一点。