Friday, August 29, 2014

Update Handlers Section in Web Config through PowerShell

Hi,

There was a requirement of updating our timer job config with the below values in app settings section.
Post that we are referring the values in our web part.

<add name="HttpHandler.Layouts.HttpHandler.MyCustomHandlerDocx" verb="*" path="*.docx" type="HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9" resourceType="Unspecified" />
      <add name="HttpHandler.Layouts.HttpHandler.MyCustomHandlerDoc" verb="*" path="*.doc" type="HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9" resourceType="Unspecified" />
      <add name="HttpHandler.Layouts.HttpHandler.MyCustomHandlerPPTX" verb="*" path="*.pptx" type="HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9" resourceType="Unspecified" />
      <add name="HttpHandler.Layouts.HttpHandler.MyCustomHandlerPPT" verb="*" path="*.ppt" type="HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9" resourceType="Unspecified" />
      <add name="HttpHandler.Layouts.HttpHandler.MyCustomHandlerXLSX" verb="*" path="*.xlsx" type="HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9" resourceType="Unspecified" />
      <add name="HttpHandler.Layouts.HttpHandler.MyCustomHandlerXLS" verb="*" path="*.xls" type="HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9" resourceType="Unspecified" />
      <add name="HttpHandler.Layouts.HttpHandler.MyCustomHandlerPDF" verb="*" path="*.pdf" type="HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9" resourceType="Unspecified" />
      <add name="HttpHandler.Layouts.HttpHandler.MyCustomHandlerJPG" verb="*" path="*.jpg" type="HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9" resourceType="Unspecified" />

Now for these entries we had created the below PowerShell script

if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null )
{
            Add-PSSnapin Microsoft.SharePoint.Powershell
}
Write-Host "Entered???"
$WebAppConfigPath = "C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config"
Write-Host "Loading Web.Config as XML in " $WebAppConfigPath -ForegroundColor Cyan
[xml]$xml = Get-Content $WebAppConfigPath
Write-Host "Started" -ForegroundColor Green
$handlersNode =$xml.SelectSingleNode("//configuration/system.webServer/handlers")

if ($handlersNode -eq $null)
{
Write-Host "Inside if loop" -ForegroundColor Green
$addhandlersNode=$xml.CreateNode('element',"handlers","")
$handlersNode=$xml.SelectSingleNode("//configuration/system.webServer").AppendChild($addhandlersNode)

}

##region to add entries into Handlers section.
function AddFileFormats()
{
try
{
$config1=$xml.CreateNode('element',"add","")
$config1.SetAttribute('name','HttpHandler.Layouts.HttpHandler.MyCustomHandlerDocx')
$config1.SetAttribute('verb','*')
$config1.SetAttribute('path','*.docx')
$config1.SetAttribute('type','HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9')
$config1.SetAttribute('resourceType','Unspecified')

$config2=$xml.CreateNode('element',"add","")
$config2.SetAttribute('name','HttpHandler.Layouts.HttpHandler.MyCustomHandlerDoc')
$config2.SetAttribute('verb','*')
$config2.SetAttribute('path','*.doc')
$config2.SetAttribute('type','HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9')
$config2.SetAttribute('resourceType','Unspecified')

$config3=$xml.CreateNode('element',"add","")
$config3.SetAttribute('name','HttpHandler.Layouts.HttpHandler.MyCustomHandlerPPTX')
$config3.SetAttribute('verb','*')
$config3.SetAttribute('path','*.pptx')
$config3.SetAttribute('type','HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9')
$config3.SetAttribute('resourceType','Unspecified')

$config4=$xml.CreateNode('element',"add","")
$config4.SetAttribute('name','HttpHandler.Layouts.HttpHandler.MyCustomHandlerPPT')
$config4.SetAttribute('verb','*')
$config4.SetAttribute('path','*.ppt')
$config4.SetAttribute('type','HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9')
$config4.SetAttribute('resourceType','Unspecified')

$config5=$xml.CreateNode('element',"add","")
$config5.SetAttribute('name','HttpHandler.Layouts.HttpHandler.MyCustomHandlerXLSX')
$config5.SetAttribute('verb','*')
$config5.SetAttribute('path','*.xlsx')
$config5.SetAttribute('type','HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9')
$config5.SetAttribute('resourceType','Unspecified')

$config6=$xml.CreateNode('element',"add","")
$config6.SetAttribute('name','HttpHandler.Layouts.HttpHandler.MyCustomHandlerXLS')
$config6.SetAttribute('verb','*')
$config6.SetAttribute('path','*.xls')
$config6.SetAttribute('type','HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9')
$config6.SetAttribute('resourceType','Unspecified')

$config7=$xml.CreateNode('element',"add","")
$config7.SetAttribute('name','HttpHandler.Layouts.HttpHandler.MyCustomHandlerPDF')
$config7.SetAttribute('verb','*')
$config7.SetAttribute('path','*.pdf')
$config7.SetAttribute('type','HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9')
$config7.SetAttribute('resourceType','Unspecified')

$config8=$xml.CreateNode('element',"add","")
$config8.SetAttribute('name','HttpHandler.Layouts.HttpHandler.MyCustomHandlerJPG')
$config8.SetAttribute('verb','*')
$config8.SetAttribute('path','*.jpg')
$config8.SetAttribute('type','HttpHandler.Layouts.HttpHandler.MyCustomHandler, HttpHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 6a49ec3306142af9')
$config8.SetAttribute('resourceType','Unspecified')
$handlersNode.AppendChild($config1)
$handlersNode.AppendChild($config2)
$handlersNode.AppendChild($config3)
$handlersNode.AppendChild($config4)
$handlersNode.AppendChild($config5)
$handlersNode.AppendChild($config6)
$handlersNode.AppendChild($config7)
$handlersNode.AppendChild($config8)
$xml.Save($WebAppConfigPath)
Write-Host "Completed" -ForegroundColor Green
}
catch
{
   Write-Output $_
}

}AddFileFormats
#EndRegion
Finally it is inserting the values as below

No comments:

Post a Comment