views:

259

answers:

6

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.

A: 

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.

Russ
Thanks for the link. The most popular answer on that post recommends an add-in solution. Looks interesting, but I'd still rather have my config changes deployed with my app at the same time.
Wallace Breza
The link just references a front-end add-on to make it easy to manage WebConfigModifications. I personally manage them using a few PS1 scripts and a csv formatted list of the config options. I'll include an example below.
Russ
+1  A: 

Put this in a fole 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
Russ
Thanks for the detailed post. I'll try this out.
Wallace Breza
This all looks great and I'm sure it will work. But it's still an additional step. Ideally I'd like everything to be bundled within my 1 wsp package for easy client deployment.
Wallace Breza
If it helps, you only need to run the script once. Afterwhich each time you upgrade the wsp, SharePoint apply's the web config modifications for you.I hear what your saying though and hopefully someone else can chime in with a better solution.
Russ
A: 

Russ is correct, you need to use WebConfigModifications. Here is another resource that just uses plain code.

breischl
+3  A: 

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.

Tom Vervoort
@Tom Vervoort - Thanks for the link - I'm getting an error when attempting this approach. I've added a seperate SO question to track this error @ http://stackoverflow.com/questions/3478441/sharepoint-2010-receiving-error-attempting-to-use-spwebconfigmodification
Wallace Breza
This approach seems to address all of the Wallace's concerns. This may not work if the order of entries under a node matters e.g. for HttpModule registration.
Jason Weber
A: 

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.

Wallace Breza
+1  A: 

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."

Jason Weber