How to Upload the Master Pages into SharePoint 2013 Using PowerShell

Sathish Nadarajan
 
Solution Architect
October 15, 2015
 
Rate this article
 
Views
9436

As I said in the earlier article, the script to upload the JS is as follows. I hope it does not require much explanations.

 ##================================================================================================
 ## Description	: To do the below two items.
     #1. Upload the MasterPage into GVI Site Collection
 ## Author		: Sathish Nadarajan
 ## Date			: 08-Oct-2015
 ##================================================================================================
 
 ##============================================ Setup Input Paths ===========================================================
 
 cls
  
 $Host.UI.RawUI.WindowTitle = "-- Upload the MasterPage into GVI Site Collection --"
 
 $StartDate = Get-Date
 Write-Host -ForegroundColor White "------------------------------------"
 Write-Host -ForegroundColor White "| Upload the MasterPage into GVI Site Collection |"
 Write-Host -ForegroundColor White "| Started on: $StartDate |"
 Write-Host -ForegroundColor White "------------------------------------"
 
 $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
 $LogFile = ".UploadCSS-$LogTime.rtf"
 
 #start-transcript $logfile
 
 $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
 Set-Location $scriptBase
 
 $ErrorActionPreference = "Stop"
 
 function AddPowerShellSnapin()
 {
     try
     {
         Write-Host "Adding PowerShell Snap-in" -ForegroundColor Green
         # Try to get the PowerShell Snappin.  If not, then adding the PowerShell snappin on the Catch Block
         Get-PSSnapin "Microsoft.SharePoint.PowerShell" 
     }
     catch
     {
         if($Error[0].Exception.Message.Contains("No Windows PowerShell snap-ins matching the pattern 'Microsoft.SharePoint.PowerShell' were found"))
         {
             Add-PSSnapin "Microsoft.SharePoint.PowerShell"
         }
     }
     Write-Host "Finished Adding PowerShell Snap-in" -ForegroundColor Green
 }
 
 #http://spdev5.dev.cbre.eu/services/GVI/Style%20Library/cbre/GVI.css
 
 function UploadDisplayTemplates([string]$siteUrl)
 {
     $spsite = Get-SPSite $siteUrl
     $web = $spsite.RootWeb
     $contentWebPartsDisplayTemplatesFolder = ($web).GetFolder("Master Page Gallery")
     $displayTemplatesDirectory = $scriptBase + "MasterPages"
     $web.AllowUnsafeUpdates=$true;
 
     #For upload all files in document library from file system
     foreach ($file in Get-ChildItem $displayTemplatesDirectory)
     {
          try
          {
                  $stream = [IO.File]::OpenRead($file.fullname)
                  $destUrl = $web.Url + "/_catalogs/masterpage/" + $file.Name;
                  $displayTemplateFile = $web.GetFile($destUrl)
                   
                  if($displayTemplateFile.CheckOutStatus -ne "None")
                  {
                      $contentWebPartsDisplayTemplatesFolder.files.Add($destUrl,$stream,$true)
                      $stream.close() 
                           
                      $displayTemplateFile.CheckIn("CheckIn by PowerShell");                         
                      $displayTemplateFile.Publish("Publish by PowerShell");                         
                       
                      $displayTemplateFile.Update();        
                      $web.Update();
                      
                      Write-Host $file.Name " Master Pages uploaded on $web site" -ForegroundColor Green  
                       
                  }
                  else
                  {
                      $displayTemplateFile.CheckOut();
                      try
                      {
                         $contentWebPartsDisplayTemplatesFolder.Files.Add($destUrl,$stream,$true)
                      }
                      catch
                      {
                         Write-Host $_
                      }
                      $stream.close()                             
                      $displayTemplateFile.CheckIn("CheckIn by PowerShell");                         
                      $displayTemplateFile.Publish("Publish by PowerShell");                         
                       
                      $displayTemplateFile.Update();        
                      $web.Update();
                      
                       
                      Write-Host $file.Name " Master Pages uploaded on $web site" -ForegroundColor Green  
                       
                  }
          }
          catch
          {
              Write-Host $_  
          }    
      }
   
   $web.AllowUnsafeUpdates  = $false;
 
  $web.dispose();
  $spsite.dispose();
 }
 
 try
 {
     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")
     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint.Administration")
     AddPowerShellSnapin
     UploadDisplayTemplates "http://spdev5.dev.cbre.eu/services/GVI" # SiteCollection URL
 }
 catch
 {
     Write-Host "Custom Exception Happened on Main : " + $Error[0].Exception.Message -ForegroundColor Red  
 }
 

This PowerShell script will Add the File for the first time, and from, the second time onwards, it will check out and check in the files. Hence, we will not lose our versioning as well.

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment