2010-11-07 91 views
0

什麼是使用代碼在SharePoint中修改web.config的最佳方式?我可以使用SPWebConfigModification或PowerShell。什麼是使用代碼修改SharePoint中的web.config的最佳方式

+0

取決於使用情況,您可以使用PowerShell,我只是寫上[通過PowerShell的修改web.config文件]博客文章(http://gavinb.net/2011/06/13 /禁用移動重定向換一個-web的應用程序通的powershell /) – GavinB 2011-06-17 06:18:31

回答

1

你可以從VS 2010的代碼的web.config修改。請參閱下面的代碼以獲取相同的示例。我實施了

  //Add tagMapping 
      string modificationName = string.Format(@"add[@tagType='Microsoft.SharePoint.WebControls.PeopleEditor'][@mappedTagType='PeopleEditor.CustPeopleFind,{0}']", 
       Assembly.GetExecutingAssembly().FullName); 
      SPWebConfigModification modification = new SPWebConfigModification //(modificationName, "configuration/system.web/pages/tagMapping"); 
      { 
       Path = "configuration/system.web/pages/tagMapping", 
       Name = modificationName, 
       Value = string.Format(@"<add tagType=""Microsoft.SharePoint.WebControls.PeopleEditor"" mappedTagType=""PeopleEditor.CustPeopleFind"" />"), 
       Owner = ownerID, 
       Sequence = 1, 
       Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode      
      }; 
      webApp.WebConfigModifications.Add(modification); 

      //Save changes 
      webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); 

      //Serialize and propagate changes across farm 
      webApp.Update(); 

      //Create the persistent objects 
      Common.CreatePersistentObjects(webApp); 

但是,我也應該警告你,許多SharePoint專家不建議通過代碼對web.config進行修改。

所有最優秀的

-Vighnesh

2

如果要使用代碼執行此操作,SPWebConfigModification將是一個不錯的選擇,因爲這樣可以處理場中所有前端Web服務器上的web.config修改。通過PowerShell執行它幾乎就像手動修改它,這對於SharePoint環境來說是令人沮喪的做法。

如果您使用的是FEATURES,您可能希望將代碼放入Feature Receivers。

如果聲明性修改滿足您的需要,那麼您可能更喜歡(通過代碼方法)。像SafeControl這樣的條目本來就受到Feature框架的支持,它應該是這些條目的首選。聲明修改的另一個變化是通過補充config文件,如下所述:

http://msdn.microsoft.com/en-us/library/ms460914.aspx

相關問題