1

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?

3 Answers 3

1

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

1
  • Thanks for help ! I'm going to use SPWebConfigModification ... it looks pretty easy to implement and understandable
    – Peter P
    Commented May 16, 2012 at 8:35
2

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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