How to Upload MasterPages, Display Templates into SharePoint 2013 using PowerShell

Sathish Nadarajan
 
Solution Architect
February 11, 2015
 
Rate this article
 
Views
16923

In many situations, we would have faced with this kind of requirement. i.e., Upload the Display Templates frequently without deploying the WSPs during our development phase. In those scenarios, this article will be very much helpful.

During our development phase, we require to upload the display templates many times. In that case, deploying the WSPs will be very much time consuming. Hence, we can create a PowerShell Script Handy and modify the display template, run this script, this will upload the display templates to appropriate folder. Refresh the search webparts. Really simple right.. Let us see the script.

 $webAppUrl = "http://c4968397007/" # Web Application URL
 
 
 
 [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")
 [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint.Administration")
 
 $checkInComment="Check In by PowerShell"
 $publishComment="published by PowerShell"
 $approveComment="Approved by PowerShell"
 
 $logfile = "UploadMasterPage_$(get-date -f yyyyMMdd_hhmmss).log"
 $spsite = new-object Microsoft.Sharepoint.SPSite($webAppUrl);
 $web = $spsite.RootWeb;
  
 $masterPageList = ($web).GetFolder("Content Web Parts")
 
 # Get file system path
 $filesfolde = Split-Path $script:MyInvocation.MyCommand.Path
 $masterPageLocalDir = $filesfolde + "Docs"
 
 #For upload all files in document library from file system
 foreach ($file in Get-ChildItem $masterPageLocalDir)
 	{
 	    $web.AllowUnsafeUpdates=$true;
         try
         {
 	        if ([Microsoft.SharePoint.Publishing.PublishingSite]::IsPublishingSite($spsite)) 
 	        {
 	
 	            $stream = [IO.File]::OpenRead($file.fullname)
                 $destUrl = $web.Url + "/_catalogs/masterpage/Display Templates/Content Web Parts/" + $file.Name;
 		        $masterPageFile=$web.GetFile($destUrl)
 	            
                 if($masterPageFile.CheckOutStatus -ne "None")
 		        {
 				
                     $web.AllowUnsafeUpdates  = $true;
                     $masterPageList.files.Add($destUrl,$stream,$true)
 				    $stream.close()						
                     $masterPageFile.CheckIn($checkInComment);						
                     $masterPageFile.Publish($publishComment);				
                     $masterPageFile.Approve($approveComment);
                     $masterPageFile.Update();         
                     $web.Update();
                     $web.AllowUnsafeUpdates  = $false;
                     $outputText = $file.Name+ " Master Page uploaded on $web site"
                     write-output $outputText
                     write-output $outputText |  out-File $logfile -Append
 		        }
 	            else
 		        {
                     $masterPageFile.CheckOut();
                     try{
                     $masterPageList.Files.Add($destUrl,$stream,$true)
                     }
                     catch
                     {
                     write-Output $_
                     }
                     $stream.close()							  
                     $masterPageFile.CheckIn($checkInComment);						  
                     $masterPageFile.Publish($publishComment);						  
                     $masterPageFile.Approve($approveComment);
                     $masterPageFile.Update();         
                     $web.Update();
                     $web.AllowUnsafeUpdates  = $false;
                     $outputText = $file.Name +  " Master Page uploaded on $web site"
                     write-output $outputText
                     write-output $outputText |  out-File $logfile -Append
 		        }
 	        }
 	
 	        else
 	        {
 	
                 $stream = [IO.File]::OpenRead($file.fullname)
                 $destUrl = $web.Url + "/_catalogs/masterpage/Display Templates/Content Web Parts/" +$file.Name
                 $masterPageFile=$web.GetFile($destUrl)
                 if($masterPageFile.CheckOutStatus -ne "None")
                 {
                     $masterPageList.Files.Add($destUrl,$stream,$true)
                     $stream.close()						
                     $masterPageFile.CheckIn($checkInComment);						
                     $masterPageFile.Publish($publishComment);				
                     $masterPageFile.Approve($approveComment);
                     $masterPageFile.Update();         
                     $web.Update();
                     $web.AllowUnsafeUpdates  = $false;
                     $outputText = $file.Name +  "Master Page uploaded on $web site"
                     write-output $outputText
                     write-output $outputText |  out-File $logfile -Append
                 }
                 else
                 {
                     $masterPageFile.CheckOut();
                     $masterPageList.Files.Add($destUrl,$stream,$true)
                     $stream.close()							  
                     $masterPageFile.CheckIn($checkInComment);						  
                     $masterPageFile.Publish($publishComment);						  
                     $masterPageFile.Approve($approveComment);
                     $masterPageFile.Update();         
                     $web.Update();
                     $web.AllowUnsafeUpdates  = $false;
                     $outputText = $file.Name+ "Master Page uploaded on $web site"
                     write-output $outputText
                     write-output $outputText |  out-File $logfile -Append
                 }
 	        }
 	
 	
         }
         catch
         {
             write-Output $_ | out-File $logfile -Append
         }	 
     }
 
 $web.dispose();
 $spsite.dispose();
 

DOWNLOAD HERE

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