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