WebConfigModifications adding and removing


I have had loads of problems in the past and recently when it comes to updating the Web.Config of a SharePoint Web Application. Today with the help of the blog http://blogs.devhorizon.com/reza/2008/01/05/spwebconfigmodifications-top-6-issues/ by Reza Alirezaei, I think I have worked out why it never works. It’s all to do with the Name.

Using PowerShell I’m trying to add a connection string to a SQL Database to my Web.Config. First I want to create the section in my web.config of <connectionStrings>. This might already exist, but I have found that if it does already exist it will not be duplicated.


#MyWebApplication URL

$myHandlerWebApplication = "http://intranet.cann0nf0dder.com"

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get and Load SharePoint dll.

$sharePointDLL = $ENV:CommonProgramFiles+("\Microsoft Shared","Web Server Extensions","14","ISAPI" -join "\") + "\Microsoft.SharePoint.dll"

[System.Reflection.Assembly]::LoadFile($sharePointDLL) | Out-Null

#Get Web Application.

$webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($myHandlerWebApplication)

#Create the ConnectionString Section.

#new SPWebConfigModification

$connectionPath = New-Object "Microsoft.SharePoint.Administration.SPWebConfigModification"

#XPath to the section.

$connectionPath.Path = "configuration"

#Name of the the SPWebConfigModification

$connectionPath.Name = "connectionStrings"

#Order – 0 means first, lower number will place further down.

$connectionPath.Sequence = 0

#Owner of the SPWebConfigModification

$connectionPath.Owner = "cann0nF0dderMyDemoConnectionSettings"

#Type can be 0 - EnsureChildNode, 1 - EnsureAttribute, 2 - EnsureSection

$connectionPath.Type = 2

#Add Modification to collection.

$webApp.WebConfigModifications.Add($connectionPath)

The above code will create a section similar to below

<connectionStrings></connectionStrings>
</configuration>

Underneath the above code we will write a ChildNode which will contain our actual connection string. (Please don’t run the following in your environment, you won’t be able to remove by code)


$connectionString = New-Object "Microsoft.SharePoint.Administration.SPWebConfigModification"

$connectionString.Path = "configuration/connectionStrings"

$connectionString.Name = "MyConnectionStringName"

$connectionString.Sequence = 0

$connectionString.Type = 0

$connectionString.Owner = " cann0nF0dderMyDemoConnectionSettings "

$connectionString.Value = "<add name='MyConnectionStringName' connectionString='server=SQLServer1;Database=Database1;Trusted_Connection=True' />"

$webapp.WebConfigModifications.Add($connectionString)

#Update the Web Application and apply all existing web.config modifications

$webapp.Update()

$webapp.Parent.ApplyWebConfigModifications()

 

The above code will add an entry to your web.config, however if you ever want to remove it by code it won’t remove. The reason, I discovered, is to do with the line $connectionString.Name = “MyConnectionStringName”. The Name parameter used is not really the names. They are relative xpaths to the modification. If these are not correctly defined, when you run the code to remove, it might actually remove the value from the $webapp.WebConfigModifications collection, it doesn’t remove it from the actual Web.Config. Therefore in my case the name should be


$connectionString.Name = "add[@name='MyConnectionStringName']"

To remove the code, the best way is to grab the webConfigModifications collection where owner is x. Then loop through and delete.


$myHandlerWebApplication = "http://intranet.cann0nf0dder.com "

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$sharePointDLL = $ENV:CommonProgramFiles+("\Microsoft Shared","Web Server Extensions","14","ISAPI" -join "\") + "\Microsoft.SharePoint.dll"

[System.Reflection.Assembly]::LoadFile($sharePointDLL) | Out-Null

$webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($myHandlerWebApplication)

$Modification = $webapp.WebConfigModifications | ? { $_.Owner -eq " cann0nF0dderMyDemoConnectionSettings "}

 if($Modification -ne $null){

 write-Host "Deleting existing entries"

 #Delete them otherwise they will duplicate.

    foreach($mod in $Modification){

            $webapp.WebConfigModifications.Remove($mod)

    }

$webapp.Update()

$webapp.Parent.ApplyWebConfigModifications()

Write-Host "Complete"

Advertisements