我有一個功能,可以在激活時將自定義主頁面部署到網站集中的所有網站,並且希望在停用時刪除自定義主頁面的所有跟蹤。停用時,將網站的主頁面設置回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/");*/
}
}
}
}
不要處理要素屬性中的SPSite對象。也不要從SPSite.RootWeb中部署SPWeb。僅從SPSite.OpenWeb()。 – trgraglia 2013-05-10 11:50:16