5
votes

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.

I found some information on MSDN regarding adding supplemental .config files during deployment, but I have not been able to get this to work. 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.

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.

If you have any ideas on an alternate maintainable approach to deploying web.config changes, my ears are open.

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.

4
Regarding your supplemental file not being applied to web.config: From MSDN (msdn.microsoft.com/en-us/library/ms439965.aspx): "You can retroactively apply changes to the web.config files of the server by running the copyappbincontent Stsadm command-line operation. You must run the operation on each front-end web server in the deployment."Jason Weber
@Jason Weber - That was it, running stsadm -o copyappbincontent was the missing piece... I guess i glazed over that line in the docs. If you want to add that as an answer I will give you credit for this question.Wallace Breza

4 Answers

3
votes

Regarding your supplemental file not being applied to web.config: From MSDN: "You can retroactively apply changes to the web.config files of the server by running the copyappbincontent Stsadm command-line operation. You must run the operation on each front-end web server in the deployment."

5
votes

As Russ and breischl suggest, you can make use of the WebConfigModifications property of the SPWebApplication object. 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.

In your feature receiver, don't forget to call the ApplyWebConfigModifications() property on your SPWebApplication object.

Example: 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.

3
votes

When creating a supplemental config file, the web.config modifications are NOT automatically merged until you make a call to stsadm -o copyappbincontent.

You can also force this command to be run through a FeatureReceiver.

After exploring the stsadm tool in reflector I found that the copyappbincontent operation makes a call to 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.

1
votes

Put this in a file called 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

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:

#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