SharePoint 2010 - creating an additional web.config file

I am currently developing a new feature for SharePoint 2010. Along with deploying my feature, I would like to add some parameters to the <appSettings/> section in my SharePoint web.config applications.

I found some information about MSDN regarding the addition of additional .config files during deployment, but I could not get this to work. This approach seemed to me the cleanest since I have all my changes in one file and their deployment with the rest of my application.

I created the webconfig.MyApp.xml file, as indicated in the documentation, deployed it to the <SharePoint 14 hive>\Config folder, but my changes do not apply to my web.config applications.

Below is an example snippet from my optional configuration 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 web.config, as these changes could easily be lost during SharePoint maintenance, etc.

If you have any ideas for an alternative supportive 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 inside my only WSP and deployed without any extra steps.

+4
source share
6 answers

As for your additional file, which does not apply to web.config: From MSDN : "You can retroactively apply changes to the Internet. Server config files by running the copyappbincontent Stsadm command line operation. You must run the operation on each front-end web server during deployment. "

+3
source

As Russ and breischl say, you can use the WebConfigModifications property of the SPWebApplication object. To deploy this along with your function, put your code in a function receiver. Thus, you can automatically change the settings of web.config when you install this function.

In the function receiver, remember to call the ApplyWebConfigModifications () property in the SPWebApplication object.

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

You can pack both your function and your function receiver assembly in one wsp package.

+5
source

When you create an additional configuration file, the changes to web.config are NOT automatically merged until you make a call to stsadm -o copyappbincontent .

You can also force this command to run through FeatureReceiver .

After examining the stsadm tool in the reflector, I found that the copyappbincontent operation calls the SPWebApplication.WebService.ApplyApplicationContentToLocalServer() '

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

Suitable for @ Jason Weber to find out, but unfortunately, he put his answer in a comment, not a question.

+2
source

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 configurations, for example:

 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 used to deploy the solution, import the csv configuration parameters and call the function created in the first block of code:

 #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 
+1
source

I believe this one will help you. Using WebConfigModifications is remembered by SharePoint and automatically written to the physical web.config files for you, so it’s easy to maintain them on multiple deployed sites.

0
source

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

0
source

All Articles