使用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中成功完成了此操作,或者我應該打開反射器並嘗試查看結果?
對五歲的回答問題的投票是* super *有用。 – 2015-06-08 19:59:09