简体   繁体   English

SharePoint 2010 - 创建补充web.config文件

[英]SharePoint 2010 - Creating a supplemental web.config file

I'm currently developing a new feature for SharePoint 2010. Along with the deployment of my feature I would like to add some settings to the <appSettings/> section within my SharePoint applications web.config. 我目前正在为SharePoint 2010开发一项新功能。除了部署我的功能外,我还想在SharePoint应用程序web.config中的<appSettings/>部分添加一些设置。

I found some information on MSDN regarding adding supplemental .config files during deployment, but I have not been able to get this to work. 我在MSDN上找到了有关在部署期间添加补充.config文件的一些信息,但我无法使其工作。 This approach seemed the cleanest to me since I can have all my changes within a single file and have them deployed with the rest of my application. 这种方法对我来说似乎是最干净的,因为我可以将所有更改放在一个文件中,并将其与我的应用程序的其余部分一起部署。

I've created a webconfig.MyApp.xml file as suggested in the documentation, deployed it to the <SharePoint 14 hive>\\Config folder, but my changes are not being propagated to my applications web.config. webconfig.MyApp.xml文档中的建议创建了一个webconfig.MyApp.xml文件,并将其部署到<SharePoint 14 hive>\\Config文件夹,但我的更改未传播到我的应用程序web.config。

Below is a sample snippet from my supplemental config file. 以下是我的补充配置文件中的示例代码段。

<?xml version="1.0" encoding="utf-8" ?>
<actions>
    <add path="configuration/appSettings">
        <add key="MyFeatureKey" value="MyFeatureValue" />
    </add>
</actions>

I would like to avoid having to manually edit the web.config since those changes can easily be lost during SharePoint maintenance, etc. 我想避免手动编辑web.config,因为在SharePoint维护期间这些更改很容易丢失等。

If you have any ideas on an alternate maintainable approach to deploying web.config changes, my ears are open. 如果您对部署web.config更改的备用可维护方法有任何想法,我的耳朵是开放的。

UPDATE: The answers that have been given so far are great and I'm sure they will work. 更新:到目前为止给出的答案很棒,我相信它们会起作用。 But I'm looking for a solution that can be packaged up within my single WSP and deployed without any additional steps required. 但我正在寻找一种解决方案,可以在我的单个WSP打包并部署,无需任何额外步骤。

As Russ and breischl suggest, you can make use of the WebConfigModifications property of the SPWebApplication object. 正如Russ和breischl所建议的那样,您可以使用SPWebApplication对象的WebConfigModifications属性。 To deploy this together with your feature, put your code in a feature receiver. 要将其与您的功能一起部署,请将您的代码放入功能接收器中。 That way, you can make the web.config modifications automatically when the feature is installed. 这样,您可以在安装该功能时自动进行web.config修改。

In your feature receiver, don't forget to call the ApplyWebConfigModifications() property on your SPWebApplication object. 在功能接收器中,不要忘记在SPWebApplication对象上调用ApplyWebConfigModifications()属性。

Example: http://weblogs.asp.net/wesleybakker/archive/2009/01/21/web.config-modifications-with-a-sharepoint-feature.aspx 示例: http//weblogs.asp.net/wesleybakker/archive/2009/01/21/web.config-modifications-with-a-sharepoint-feature.aspx

You can package both your feature and your feature receiver assembly in a single wsp package. 您可以将功能和功能接收器组件打包在一个wsp包中。

When creating a supplemental config file, the web.config modifications are NOT automatically merged until you make a call to stsadm -o copyappbincontent . 创建补充配置文件时,在调用stsadm -o copyappbincontent之前, 不会自动合并 web.config修改。

You can also force this command to be run through a FeatureReceiver . 您还可以强制此命令通过FeatureReceiver运行。

After exploring the stsadm tool in reflector I found that the copyappbincontent operation makes a call to SPWebApplication.WebService.ApplyApplicationContentToLocalServer() ' 在浏览器中探索stsadm工具后,我发现copyappbincontent操作调用SPWebApplication.WebService.ApplyApplicationContentToLocalServer() '

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    var webApp = (SPWebApplication)properties.Feature.Parent;
    webApp.WebService.ApplyApplicationContentToLocalServer();
}

Props to @Jason Weber for figuring this out, but unfortunately he put his answer in a comment rather than a question. 为了解决这个问题,向@Jason Weber道具,但遗憾的是他把答案放在评论中而不是问题中。

关于未应用于web.config的补充文件:来自MSDN :“您可以通过运行copyappbincontent Stsadm命令行操作,追溯将更改应用于服务器的web.config文件。您必须在每个前端运行操作部署中的Web服务器。“

Put this in a file called AddWebConfigMods.ps1 把它放在一个名为AddWebConfigMods.ps1的文件中

Param ($weburl, $WebConfigModifications )

$url = New-Object System.Uri($webUrl)

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null
$webApp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url)


#delete mods by the owners being added
$ownerstodelete = $WebConfigModifications | Group-Object owner | Select-Object Name

foreach($owner in $ownerstodelete)
{
    $modstodelete = @()
    foreach($mod in $webApp.WebConfigModifications)
    {
        if($mod.Owner -eq $owner.Name)
        {
            $modstodelete += $mod
        }
    }

    Write-Host ("Deleting " + $modstodelete.Count + " mods for owner: " + $owner)

    foreach($delmod in $modstodelete)
    {
        Write-Host (" + Deleting " + $delmod.Value)
        $webApp.WebConfigModifications.Remove($delmod) | Out-Null
    }
}

#this is where we start to add mods
$i = 0;

Write-Host ("Adding " + $WebConfigModifications.Count + " webconfig modifications to " + $weburl) 

foreach($modEntry in $WebConfigModifications)
{
    Write-Host (" + Adding " + $modEntry.Value)

    $mod = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
    $mod.Path = $modEntry.Path
    $mod.Name = $modEntry.Name
    $mod.Sequence = $i++
    $mod.Owner = $modEntry.Owner
    $mod.Type = $modEntry.Type
    $mod.Value = $modEntry.Value

    $webApp.WebConfigModifications.Add($mod)

}

$webApp.Update()
$webApp.WebService.ApplyWebConfigModifications()

Then create a csv file with your configs like so 然后使用您的配置创建一个csv文件

Name,Path,Owner,Type,Value
system.serviceModel,configuration,alinean-common,EnsureSection,<system.serviceModel/>
connectionStrings,configuration,alinean-common,EnsureSection,<connectionStrings />
appSettings,configuration,alinean-common,EnsureSection,<appSettings />
serviceHostingEnvironment,configuration/system.serviceModel,alinean-common,EnsureChildNode,<serviceHostingEnvironment aspNetCompatibilityEnabled='true'/>

Then in another ps1 script, ideally the one you use to deploy your solution, import the csv config options and call the function created in the first code block: 然后在另一个ps1脚本中,理想情况下用于部署解决方案的脚本,导入csv配置选项并调用在第一个代码块中创建的函数:

#read config mods from CSVs
$mods = Import-CSV .\config\admin-common.webconfigmods.csv

Write-Host "Applying configuration modifications"
#add web config mods to sharepoint using powershell script
&.\AddWebConfigMods "[sharepoint site url]" $mods
&stsadm -o execadmsvcjobs

I believe this will help you out. 我相信会帮助你。 Using WebConfigModifications are remembered by SharePoint and are automatically written to the physical web.config files for you thus being easy to maintain across multiple deployed sites. 使用WebConfigModifications会被SharePoint记住,并自动写入物理web.config文件,因此可以轻松地跨多个部署的站点进行维护。

Russ is correct, you need to use WebConfigModifications. Russ是正确的,您需要使用WebConfigModifications。 Here is another resource that just uses plain code. 是另一种只使用普通代码的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM