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.
<?xml version="1.0" encoding="utf-8"?> - <Inputs>
- <ConnectSPOnline SiteURL="https://c986.sharepoint.com/sites/vijai" UserName="[email protected]" Password="xxxxxxxx"></ConnectSPOnline>
- <MasterPage SourceFolder=".\MasterPage" FileName="demo.master" Title="demo" Description="demo" Default="yes"></MasterPage>
- </Inputs>
Open Notepad and paste the script given below. Save the text file as masterpage.ps1.
- ############################################################## Logging #########################################
-
- $date= Get-Date -format MMddyyyyHHmmss
- start-transcript -path .\Log_$date.doc
-
- ################################################### Get input parameters from XML ##############################
-
- # Get content from XML file
- [xml]$xmlData=Get-Content ".\Inputs.xml"
-
- # ConnectSPOnline node
- [System.Xml.XmlElement]$connectSPOnline = $xmlData.Inputs.ConnectSPOnline
- $siteURL=$connectSPOnline.SiteURL
- $userName=$connectSPOnline.UserName
- $password=$connectSPOnline.Password
-
- # MasterPage node
- [System.Xml.XmlElement]$masterPage = $xmlData.Inputs.MasterPage
- $masterSourceFolder=$masterPage.SourceFolder
- $masterFileName=$masterPage.FileName
- $masterTitle=$masterPage.Title
- $masterDescription=$masterPage.Description
- $masterDefault=$masterPage.Default
-
- ########################################################## Get Credentials #####################################
-
- function GetCredentials()
- {
- write-host -ForegroundColor Green "Get Credentials and connect to SP Online site: " $siteURL
- # Convert password to secure string
- $secureStringPwd = ConvertTo-SecureString -AsPlainText $Password -Force
-
- # Get the credentials
- $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$secureStringPwd
-
- # Connect to SP online site
- Connect-PnPOnline –Url $siteURL –Credentials $credentials
- }
-
- ###################################################### Add and Set Master Page #################################
-
- function AddandSetMasterPage()
- {
- write-host -ForegroundColor Green "Add and set master page: " $masterFileName
-
- # Get source file path for master page
- $sourceFilePath=$masterSourceFolder+"\"+$masterFileName
-
- # Add master page to catalogs folder
- Add-PnPMasterPage -SourceFilePath $sourceFilePath -Title $masterTitle -Description $masterDescription
-
- # Check if the master page has to be set as default master page
- if($masterDefault="yes")
- {
- # Set default master page
- $masterPageSiteRelativeUrl="_catalogs/masterpage/"+$masterFileName
- $customMasterPageSiteRelativeUrl="_catalogs/masterpage/"+$masterFileName
- Set-PnPMasterPage -MasterPageSiteRelativeUrl $masterPageSiteRelativeUrl -CustomMasterPageSiteRelativeUrl $customMasterPageSiteRelativeUrl
- }
- }
-
- ################################################################# Initiate #####################################
-
- function Initiate()
- {
- write-host -ForegroundColor Green "Initiating the script.................. "
-
- # Get Credentials and connect to SP Online site
- GetCredentials
-
- # Call the required functions
- AddandSetMasterPage
-
- # Disconnect from the server
- Disconnect-PnPOnline
-
- write-host -ForegroundColor Green "Completed!!!!"
- }
-
- ################################################################################################################
-
- Initiate
- 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