Modifying web.config with Powershell

Modifying web.config with Powershell
1 vote, 5.00 avg. rating (95% score)

Introduction

In this post we will see how we can Add / Delete a node to web.config with PowerShell in SharePoint 2010.
Most of times we consider web.config as nothing but a common xml files. But, changes that are done to a node and who is the owner of a particular node in web.config will be saved in content database. So, web.config is not a simple file for SharePoint but each node is very important.

When we have a task to add a node in web.config don’t add it as xml but use the better API of SharePoint to do.

Modifying web.config file

SharePoint has a default behaviour for the site when we open it in mobile. It won’t show the branded website but it will show the default mobile application pages. Sometimes, we require to disable this behaviour.

Disabling default mobile behaviour

To disable the default mobile behaviour and let SharePoint show the same site as we seen in our system browser; set isMobileDevice node value to false in browserCaps node which is residing in web.config file.
If the node is not there then we can add the following browserCaps node in
system.web node under configuration node.

<configuration>
 <system.web>
 .
 .
 .
 .
 <browserCaps>
 <result type="System.Web.Mobile.MobileCapabilities, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
 <filter>isMobileDevice=false</filter>
 </browserCaps>
 .
 .
 .
 .
 </system.web>
 </configuration>

Script to disable the mobile behaviour

As we have checked how we can disable default mobile behaviour, with the following script we will see how to add browserCaps node to the web.config file.

try
{
    Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
    Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

	$browserCapNodeValue = "<browserCaps><result type='System.Web.Mobile.MobileCapabilities, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' /><filter>isMobileDevice=false</filter></browserCaps>"
		
	$siteUrl = "http://adisgroup"
	$webApps = Get-SPWebApplication | where {$_.Url -like $url }
	if($webApps -ne $null)
	{
	   foreach ($WebApp in $webapps) {
	   DeleteWebConfigNode $WebApp
	
	  AddWebConfigNode $WebApp "configuration/system.web" "browserCaps" $browserCapNodeValue "0"

	   $WebApp.Update()

	   $WebApp.Parent.ApplyWebConfigModifications()

	   write-host "Adding web config node..Done"
				
		}
	
	}
	else
	{
		write-host "Invalid siteurl"
	}
	
	write-host  "WebConfig Modifications completed successfully"
   
    
	
}
catch
{
	write-host $error[0]
  
}


# Removes the webconfig 'browserCaps' nodes which contains 'Adi' as owner  
function DeleteWebConfigNode($webapp) 
{ 
    $count =  $webapp.WebConfigModifications.Count 
    $foundNodes = $false
	
	$webmodif = $WebApp.WebConfigModifications
	$webModifCount = $webmodif.Count;
	for($lCount = 0; $lCount -lt $webModifCount; $lCount++)
	{
		$webConfigChange = $webmodif[$lCount]
		if($webConfigChange -ne $null)
		{
			
			$ConfigOwner = $webConfigChange.Owner
			if($ConfigOwner -like "*Adi*")
			{
				$ConfigName = $webConfigChange.Name
				if($ConfigName -eq "browserCaps")
				{
					
					$foundNodes = $true;
					$WebApp.WebConfigModifications.Remove( $webConfigChange )        
					
				}
			}
			
		}
	}
	
	if($foundNodes -eq $true)
	{
		$WebApp.Update()
		#$WebApp.Parent.ApplyWebConfigModifications()
	}
	
	write-host "---- Removed successfully"
	
}




# function to Create Web Config node
function AddWebConfigNode ($webapp, $path, $name, $value, $type)
{   

    $childNode = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification;
    $childNode.Owner = $owner;
    $childNode.Path = $path;
    $childNode.Name = $name;
    $childNode.Value = $value;
    $childNode.Type = $type;
    $childNode.Sequence = 0
 
    if($webapp.WebConfigModifications.Contains($childNode) -eq $false){
        $webapp.WebConfigModifications.Add($childNode);
        write-host("added");
    }
} 

When we add any node to web.config file, it will also save the owner of the node. So, if we modify config node manually the owner for the node
will be empty. Also, if we add any node programatically it will take the owner value what ever we give.
Owner for node makes it more affective to identify who added particular node.
Whenever we try to add a node to webconfig, check if the node already exists with the owner. If it exists delete that node and add it again.
Note: If there are duplicate nodes in web.config the whole application will be down.

Conclusion

Hope we got better understanding on how to modify web.config and take advantage of SharePoint API rather than just modifying xml. Also the above example provides how to disable the default behavior for mobile devices.

April 27, 2012 В· Adi В· No Comments
Tags: , , ,  В· Posted in: Powershell, Sharepoint 2010

Leave a Reply

What is 4 + 5 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)