我很确定仅使用包文件/ CAML无法完成 web.config 修改。
但是,可以做的是部署一个 WebApplication Feature Reciever,它通过SPWebApplication.WebConfigModifications修改 web.config 。
这是我项目中的代码片段,有关更多详细信息,请参阅代码项目 KB:(第一个部分只是一个带有一些注释的方便函数。)
// For WebConfigModifications access,
// see http://www.codeproject.com/KB/sharepoint/SPWebConfigModTool.aspx
// Hints:
// app.WebConfigModifications.Add(new SPWebConfigModification
// {
// Type = [add/update child node?]
// Path = [XPath of parent node]
// Name = [XPath to identify child node UNIQUELY]
// Owner = [Use GUID to identify as ours]
// Sequence = [Sequence number, likely 0 for only one]
// Value = [XML node to add/update]
// });
void ModfiyWebConfig (SPWebApplication app, string path, string name, XElement node)
{
app.WebConfigModifications.Add(new SPWebConfigModification
{
Type = SPWebConfigModificationType.EnsureChildNode,
Path = path,
Name = name,
Owner = OwnerId,
Sequence = 0,
Value = node.ToString(),
});
}
获取/初始化 SPWebApplication
var app = properties.Feature.Parent as SPWebApplication;
队列/设置修改
ModfiyWebConfig(app,
"configuration/system.webServer/modules",
"add[@name='ASPxHttpHandlerModule']",
new XElement("add",
new XAttribute("name", "ASPxHttpHandlerModule"),
new XAttribute("type", aspxHandlerModule)));
应用修改
app.WebService.ApplyWebConfigModifications();
app.Update();