Wednesday 3 December 2014

Dynamically Change Master Page Path in Particular SharePoint Content Pages (EditForm.aspx , NewForm.aspx & DisplayForm.aspx) using Powershell .

Hi Friend's in this article i am trying to achieve to write a script that will change MasterPageFile URL  for all Content  Pages (EditForm.aspx , NewForm.aspx & DisplayForm.aspx) on all  Particular Site Collections in a web application.

the above can be achieve both pragmatically and in power shell....u can see below image after successfully updated

the below code u will get basic idea how to change using both powershell and server object model.

Powershell script 

$spSourceWeb = Get-SPWeb "url";          # pass site url
$page = $spSourceWeb.GetFile(url)          # pass file url
$filebytes = $page.OpenBinary()
$filestring = [System.Text.Encoding]::UTF8.GetString($filebytes)
$replacedstring  = $filestring.Replace("~masterurl/default.master","site/_catalogs/masterpage/seattle.master")
$page.SaveBinary([System.Text.Encoding]::UTF8.GetBytes($replacedstring))

---------------------------------------------------------------------------------------------------------
Programatically

Dim _site As SPSite = New SPSite("URL")
Dim _web As SPWeb = _site .OpenWeb()
Dim _list  As SPList = _web .Lists("Demo")
Dim _formcollection As SPFormCollection = _list  .Forms
For Each _f As SPForm In _formcollection 

Dim _spformfile As SPFile = _ww.GetFile(_web Url + "/" + _f.Url)
Dim data As Byte() = _spformfile.OpenBinary()
Dim s As String = Encoding.UTF8.GetString(data)
Dim replaced As String = s.Replace("~masterurl/default.master", ~site/_catalogs/masterpage/seattle.master")
 _spformfile.SaveBinary(Encoding.UTF8.GetBytes(replaced))

Next




---------------------------------------------------------------------------------------------------------

#first clear whole screen    
Clear-Host            
#add cmdlst  for farm    
Add-PSSnapin "Microsoft.SharePoint.PowerShell"


$WebApplicationUrl =  "http://ServerUrl/"
$webApplication = Get-SPWebApplication $WebApplicationUrl;
if($webApplication -ne $null)
{
foreach($site in $webApplication.sites){                 
If ($site -ne $null -and  $site.url.contains("teamrooms") ){   
$sourceWebURL = $site.url
$spSourceWeb = Get-SPWeb $sourceWebURL;
Write-Host  "-------------------" -ForegroundColor red;
Write-Host  $site -ForegroundColor red;
Write-Host  "-------------------" -ForegroundColor red;
foreach($list in $spSourceWeb.Lists){             
If ($list.Title  -eq  "Document"){  
Write-Host "List Name: "$list.Title  -ForegroundColor White;
Write-Host  "-------------------" -ForegroundColor White;
foreach($form in $list.Forms) {                
If ($form.type -eq "PAGE_EDITFORM" -or  $form.type -eq "PAGE_NEWFORM" -or $form.type -eq "PAGE_DISPLAYFORM" ){  
                     
$page = $spSourceWeb.GetFile($spSourceWeb.Url + "/" + $form.Url)
#First, we make the checkout to the page.
if ($page.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout){                               
#Undo-CheckOut in case the page were already cheked-out 
$page.UndoCheckOut(); $page.Update(); $page.CheckOut(); $page.Update();  }                                                                         
                        
#Replace master Page url in edit and new form pages 
$filebytes = $page.OpenBinary()
$filestring = [System.Text.Encoding]::UTF8.GetString($filebytes)
$replacedstring  = $filestring.Replace("~masterurl/default.master", "~site/_catalogs/masterpage/seattle.master")
$page.SaveBinary([System.Text.Encoding]::UTF8.GetBytes($replacedstring))
if ($page.ForceCheckout) { $page.CheckIn() }    
Write-Host $form.type  "-Suceesfully Replace Masterpage url" -ForegroundColor red;   }   } }  } } }  }                                                                                        
else
{
    Write-Host "Could not find Web Application $WebApplicationUrl" -ForegroundColor Red;
}

        
Share:

No comments:

Post a Comment

© TBGsharepointforum All rights reserved | Designed by Blogger Templates