SharePoint PnP PowerShell - How To Add And Set Master Page In SharePoint Online Using Powershell

Introduction

SharePointPnP.PowerShell solution contains a library of PowerShell commands, which allows you to perform complex provisioning and artifact management actions towards SharePoint. The commands use CSOM and can work against both SharePoint Online as SharePoint On-Premises.

Prerequisites

Install SharePointPnPPowerShellOnline.msi for SharePoint Online.

Add and set master page

Open Notepad and paste XML given below. Save the text file as Inputs.xml.

  1. <?
    xml version="1.0" encoding="utf-8"?>  
  2. <Inputs>  
  3.   <ConnectSPOnline SiteURL="https://c986.sharepoint.com/sites/vijai" UserName="[email protected]" Password="xxxxxxxx"></ConnectSPOnline>  
  4.   <MasterPage SourceFolder=".\MasterPage" FileName="demo.master" Title="demo" Description="demo" Default="yes"></MasterPage>  
  5. </Inputs>  

Open Notepad and paste the script given below. Save the text file as masterpage.ps1.

  1. ############################################################## Logging ######################################### 
  2.   
  3. $date= Get-Date -format MMddyyyyHHmmss  
  4. start-transcript -path .\Log_$date.doc   
  5.  
  6. ################################################### Get input parameters from XML ############################## 
  7.  
  8. # Get content from XML file  
  9. [xml]$xmlData=Get-Content ".\Inputs.xml"  
  10.  
  11. # ConnectSPOnline node  
  12. [System.Xml.XmlElement]$connectSPOnline = $xmlData.Inputs.ConnectSPOnline  
  13. $siteURL=$connectSPOnline.SiteURL  
  14. $userName=$connectSPOnline.UserName  
  15. $password=$connectSPOnline.Password  
  16.  
  17. # MasterPage node  
  18. [System.Xml.XmlElement]$masterPage = $xmlData.Inputs.MasterPage  
  19. $masterSourceFolder=$masterPage.SourceFolder  
  20. $masterFileName=$masterPage.FileName  
  21. $masterTitle=$masterPage.Title  
  22. $masterDescription=$masterPage.Description  
  23. $masterDefault=$masterPage.Default  
  24.  
  25. ########################################################## Get Credentials #####################################
  26.   
  27. function GetCredentials()  
  28. {   
  29.     write-host -ForegroundColor Green "Get Credentials and connect to SP Online site: " $siteURL  
  30.     # Convert password to secure string    
  31.     $secureStringPwd = ConvertTo-SecureString -AsPlainText $Password -Force  
  32.  
  33.     # Get the credentials  
  34.     $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$secureStringPwd   
  35.  
  36.     # Connect to SP online site  
  37.     Connect-PnPOnline –Url $siteURL –Credentials $credentials      
  38. }  
  39.  
  40. ###################################################### Add and Set Master Page ################################# 
  41.   
  42. function AddandSetMasterPage()  
  43. {  
  44.     write-host -ForegroundColor Green "Add and set master page: " $masterFileName  
  45.  
  46.     # Get source file path for master page  
  47.     $sourceFilePath=$masterSourceFolder+"\"+$masterFileName   
  48.      
  49.     # Add master page to catalogs folder     
  50.     Add-PnPMasterPage -SourceFilePath $sourceFilePath -Title $masterTitle -Description $masterDescription  
  51.  
  52.     # Check if the master page has to be set as default master page  
  53.     if($masterDefault="yes")  
  54.     {   
  55.         # Set default master page  
  56.         $masterPageSiteRelativeUrl="_catalogs/masterpage/"+$masterFileName  
  57.         $customMasterPageSiteRelativeUrl="_catalogs/masterpage/"+$masterFileName  
  58.         Set-PnPMasterPage -MasterPageSiteRelativeUrl $masterPageSiteRelativeUrl -CustomMasterPageSiteRelativeUrl $customMasterPageSiteRelativeUrl  
  59.     }  
  60. }  
  61.  
  62. #################################################################  Initiate #####################################
  63.   
  64. function Initiate()  
  65. {  
  66.      write-host -ForegroundColor Green "Initiating the script.................. "   
  67.  
  68.      # Get Credentials and connect to SP Online site  
  69.      GetCredentials  
  70.  
  71.      # Call the required functions  
  72.      AddandSetMasterPage  
  73.  
  74.      # Disconnect from the server  
  75.      Disconnect-PnPOnline  
  76.   
  77.      write-host -ForegroundColor Green "Completed!!!!"   
  78. }  
  79.  
  80. ################################################################################################################ 
  81.   
  82. Initiate  
  83. Stop-Transcript  

Create a folder named MasterPage and add the master page file to the folder.

 

Run Windows PowerShell as an administrator.

Navigate to the folder, where XML and ps1 files are available.

Type .\masterpage.ps1 and click enter.



Result

Master page needs to be added and set as default in SharePoint Online site. In this blog, you have seen how to add and set master page in SharePoint Online, using PowerShell.

Reference

  • https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/AddPnPMasterPage.md
  • https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/SetPnPMasterPage.md