2010-10-04 41 views
-1

使用SharePoint 2010我嘗試使用SPWebConfigModification類對服務器場中的應用程序(包括管理中心web.config文件)上的web.config文件進行一些基本更改使用Web應用程序作用域具有特徵的接收器功能處理添加/移除的修改:SPWebConfigModification類不會更新中央管理應用程序的web.config文件

public override void FeatureActivated(SPFeatureReceiverProperties properties) 
    { 
     SPWebApplication webApp = properties.Feature.Parent as SPWebApplication; 
     String ownerId = properties.Feature.DefinitionId.ToString(); 
     List<SPWebConfigModification> modsToAdd = new List<SPWebConfigModification>(); 

     #region Authentication Providers 

     modsToAdd.Add(new SPWebConfigModification() 
     { 
      Name = "defaultProvider", 
      Owner = ownerId, 
      Path = "configuration/system.web/membership", 
      Sequence = 0, 
      Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute, 
      Value = "FBA_AD_MP" 
     }); 

     modsToAdd.Add(new SPWebConfigModification() 
     { 
      Name = "add [@name=\"FBA_AD_MP\"] [@type=\"System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\"] [@connectionStringName=\"ADFBAConnectionString\"] [@enableSearchMethods=\"true\"] [@attributeMapUsername=\"userPrincipalName\"]", 
      Owner = ownerId, 
      Path = "configuration/system.web/membership/providers", 
      Sequence = 0, 
      Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, 
      Value = "<add name=\"FBA_AD_MP\" type=\"System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\" connectionStringName=\"ADFBAConnectionString\" enableSearchMethods=\"true\" attributeMapUsername=\"userPrincipalName\" />" 
     }); 

     #endregion 

     #region Connection Strings 

     modsToAdd.Add(new SPWebConfigModification() 
     { 
      Name = "connectionStrings", 
      Owner = ownerId, 
      Path = "configuration", 
      Sequence = 0, 
      Type = SPWebConfigModification.SPWebConfigModificationType.EnsureSection, 
      Value = "<connectionStrings />" 
     }); 

     modsToAdd.Add(WebConfigModificationsUtility.CreateConnectionStringModification(ownerId, 1, "ADFBAConnectionString", properties.Feature.Properties["ADFBAConnectionString"].Value)); 

     #endregion 

     WebConfigModificationsUtility.AddWebConfigModifications(webApp, modsToAdd.ToArray()); 
    } 

的WebConfigModificationsUtility類是通過這些特徵的接收器共享一個簡單的工具:

public static void AddWebConfigModifications(SPWebApplication webApp, params SPWebConfigModification[] modificationsToAdd) 
    { 
     AddWebConfigModifications(webApp, true, modificationsToAdd); 
    } 
public static void AddWebConfigModifications(SPWebApplication webApp, Boolean persistChanges, params SPWebConfigModification[] modificationsToAdd) 
    { 
     foreach (SPWebConfigModification curMod in modificationsToAdd) 
     { 
      SPWebService.ContentService.WebApplications[webApp.Id].WebConfigModifications.Add(curMod);     
     } 

     if (persistChanges) 
     { 
      PersistWebConfigModifications(webApp); 
     } 
    } 
public static void PersistWebConfigModifications(SPWebApplication webApp) 
    { 
     SPWebService.ContentService.WebApplications[webApp.Id].Update();       
     SPWebService.ContentService.ApplyWebConfigModifications(); 
    } 

所有內容應用程序都正常工作,但嘗試激活中央管理Web應用程序上的功能時,SPWebConfigModificaiton項目永遠不會寫入web.config文件。我已經驗證代碼正在執行,沒有例外。此外,我的功能接收器試圖刪除任何修改都已經在停用提出:

public override void FeatureDeactivating(SPFeatureReceiverProperties properties) 
     { 
      SPWebApplication webApp = properties.Feature.Parent as SPWebApplication; 
      String ownerId = properties.Feature.DefinitionId.ToString(); 
      WebConfigModificationsUtility.RemoveWebConfigModifications(webApp, ownerId); 
     } 
public static void RemoveWebConfigModifications(SPWebApplication webApp, String ownerId, Boolean persistChanges) 
     {    
      RemoveWebConfigModifications(webApp, persistChanges, webApp.WebConfigModifications.Where(x => x.Owner == ownerId).ToArray()); 
     } 
public static void RemoveWebConfigModifications(SPWebApplication webApp, Boolean persistChanges, params SPWebConfigModification[] modificationsToRemove) 
     { 
      foreach (SPWebConfigModification curMod in modificationsToRemove) 
      { 
       SPWebService.ContentService.WebApplications[webApp.Id].WebConfigModifications.Remove(curMod);     
      } 

      if (persistChanges) 
      { 
       PersistWebConfigModifications(webApp); 
      } 
     } 

當這個代碼就可以發現在功能激活創建的四個改進了Central Admin Web應用程序運行,併成功地將其刪除,但這兩個操作都不是實際上是在更改web應用程序的web.config文件;它甚至沒有被觸及,最後編輯日期的時間戳始終保持相同。

我發現各種博客都在談論SPWebConfigModification類的繼承技巧,大多數問題都圍繞正確使用名稱和所有者屬性,以及使用SPWebService靜態屬性正確保留更改,但盡我所能說我遵循所有推薦的做法。我開始懷疑API中存在一個錯誤,但是我想知道是否有人在SharePoint 2010中成功完成了此操作,或者我應該打開反射器並嘗試查看結果?

+0

對五歲的回答問題的投票是* super *有用。 – 2015-06-08 19:59:09

回答

1

就像ContentService一樣,SPWebService有一個AdministrationService對象,它也有一個WebApplications集合。使用AdministrationService訪問您的CentralAdmin Web應用程序。

-1

SPWebConfigMondification不會更新管理中心網站。

+0

是否有任何有關不與管理中心網站一起使用的課程的文檔參考?在API文檔中沒有什麼特別的,在這篇文章中至少有一些間接的證據:http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/6fe3529d-9f15-4fbd-b56e-15c738ff7cb4在SharePoint 2007中工作的MSDN論壇上。 – 2010-10-05 16:14:15

相關問題