Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have SharePoint app which we are deploying to another SharePoint server and I need to update target server web.config with changes made on source server web.config. Can somebody advise me how to perform it?

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

You can use the following PowerShell script:

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue" 

#connect to the function library for this script 
$functionLocation = "C:\PowerShell\WebConfigModifications" 
. $functionLocation\Web_Config_Modifications_Functions.ps1 

#define the SPWebConfigModification constants 
$modificationOwner = "SomeUniqueIdentifier" 
$EnsureChildNode = 0 
$EnsureAttribute = 1 

#get the web configuration modifications xml file 
$webconfigLocation = "C:\PowerShell\WebConfigModifications" 
$webconfigData = $webconfigLocation + "\Web_Config_Modifications.xml" 

# get the Web Application for the given Url 
$url = "http://YOUR-WEB-APP-URL" 
$webapp = Get-SPWebApplication $url 
$webapp 
#=============================================================================================================== 

#Write-Host "`nCleaning up existing Nodes" 

#use this statement below to correct for any errors  
#when initially adding new sections / nodes / attributes  
#etc that may cause exceptions 
#$webapp.WebConfigModifications.Clear() 

DeleteSections $webapp $modificationOwner $false 

#=============================================================================================================== 
Write-Host "`nAdding Web Config Nodes" 

#load the xml data 
[xml]$Data = get-content $webconfigData 

foreach ($Section in $Data.Sections.Section) 
{  
    #Ignore sections marked to be skipped 
    if ($Section.Skip -eq "false") 
    { 
        $path = $Section.Path 
        foreach ($modification in $Section.modifications.ChildNodes) 
        { 
            if ($modification.type -eq "EnsureAttribute") 
            { 
                $type = $EnsureAttribute 
                $name = $modification.name 
                  $value =  $modification.value  

                Write-Host "`nChanging $path -> $name to $value" 
            }             
            else 
            { 
                $type = $EnsureChildNode 
                $name = $modification.name 
                $value = "<" + $modification.value + "/>" 

                Write-Host "`nAdding Modifications -> $path : $name, $value" 
            }         

            CreateConfigChildNode $modificationOwner $type $path $name $value 
        } 
    } 
} 

#=============================================================================================================== 
Write-Host "`nApplying Modifications to the Web.Config"         
$webapp.Update() 
$webapp.Parent.ApplyWebConfigModifications() 
Write-Host "`nCompleted Web.Config Modifications" 

#----- END OF FILE  -----# 

#----- START of FUNCTION FILE ----# 

#=================================================================== 
# Web Config Modifications Script Functions 
#=================================================================== 

#=================================================================== 
# function to Create Web Config nodes and or change attributes 
#=================================================================== 
function CreateConfigChildNode($Owner, $Type, $Path, $Name, $Value) 
{ 
    $childNode = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification  
    $childNode.Owner = $Owner 
    $childNode.Path = $Path 
    $childNode.Name = $Name 
    $childNode.Value = $Value 
    $childNode.Type = $Type 

    $webapp.WebConfigModifications.Add($childNode) 
} 

#=================================================================== 
# function to remove all existing node and changes to attributes matching the Owner name 
#=================================================================== 
function DeleteSections($webapp, $Owner, $removeOnly) 
{ 
    $count =  $webapp.WebConfigModifications.Count 
    $foundNodes = $false 
    for ($i=$count ;$i -gt -1; $i--) 
    { 
        if ($webapp.WebConfigModifications[$i].Owner -eq $Owner) 
        { 
            $foundNodes = $true 
            Write-Host "`n$i" 
            $webapp.WebConfigModifications.Remove($webapp.WebConfigModifications[$i]) 
        } 
    } 

    if ($foundNodes -and $removeOnly) 
    { 
        $webapp.Parent.ApplyWebConfigModifications() 
        $webapp.Update() 
    } 
} 

#----- END OF FILE  -----# 

OR

If your SharePoint app has some custom feature then it is best to write web.config settings using SPWebConfigModification class of the Microsoft.SharePoint.Administration namespace, which allows you to dynamically register entities.

Here is example: http://msdn.microsoft.com/en-us/library/bb861909.aspx

share|improve this answer
    
Thanks for help ! I'm going to use SPWebConfigModification ... it looks pretty easy to implement and understandable –  Peter P May 16 '12 at 8:35
add comment

You code web.config changes using the SharePoint API (SPWebConfigModification) and package them into a solution file (.wsp).

Deploy the solution file to the new farm.

Do not modify the web.config file manually.

share|improve this answer
add comment

What kind of changes do you need to apply to the web.config? SharePoint applications should not store configuration settings in the web.config - it is the least desirable option among the possible ways of storing application settings in SharePoint. Wictor Wilén has a very good post on this topic.

Six ways to store settings in SharePoint

If you are asking how to apply configuration settings for your SharePoint app then I would strongly recommend to refactor your application to not depend on any settings in the web.config file.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.