2012-07-19 60 views
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/");*/ 
      } 
     } 
    } 
} 
+0

不要處理要素屬性中的SPSite對象。也不要從SPSite.RootWeb中部署SPWeb。僅從SPSite.OpenWeb()。 – trgraglia 2013-05-10 11:50:16

回答

2

您可能需要獲得一個新的參考SPWeb,並用它來打電話SPWeb.GetFile()

using塊中的SPWeb可能仍然有對custom.master的引用,並且需要刷新。

+0

試圖做與OP一樣的東西......這是否意味着在'SPWeb''使用'塊之外,我爲另一個'SPWeb'創建了另一個'using'塊?或者我應該在第一個「使用」塊內部執行它?如果我可以弄明白,或者你可以幫我解決問題,我會爲你提供另一個upvote – 2013-10-02 13:20:49

+0

爲新的SPWeb對象創建一個新的'using'塊。如果你想得到更詳細的答案,或者如果它仍然無法正常工作,請創建一個新問題併發布一些代碼,因爲你會更加關注問題。 – 2013-10-02 14:11:26

+0

謝謝 - 我最終在一個不同的模塊中完成了這項工作(只有在功能被卸載後才能刪除資產,而不是在停用時)。我的問題是,新的'using'塊是在現有塊中還是在現有'using'塊之後?可能會把它放在你的答案中是明確的(?) – 2013-10-02 17:19:06