使用SharePoint 2010我尝试使用SPWebConfigModification类对服务器场中的应用程序上的web.config文件进行一些基本更改,包括使用Web应用程序作用域功能的管理中心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.config文件Web应用程序;它甚至没有被触及,最后编辑日期的时间戳始终保持不变。
我发现各种博客都在讨论SPWebConfigModification类的继承技巧,大多数问题都是正确使用Name和Owner属性,以及使用SPWebService静态属性正确保存更改,但我最好能告诉我遵循所有推荐的做法。我开始怀疑API中存在一个错误,但我想知道是否有人在SharePoint 2010中成功完成了这项工作,或者我是否应该打开Reflector并尝试查看是什么给出的?
答案 0 :(得分:1)
就像ContentService一样,SPWebService有一个AdministrationService对象,它也有一个WebApplications集合。使用AdministrationService访问您的CentralAdmin Web应用程序。
答案 1 :(得分:-1)
SPWebConfigMondification不会更新管理中心网站。