kwizcom banner advertisement

SharePoint Online: Change Master Page using PowerShell

Requirement: Change Master Page in SharePoint Online.

How to Set Master Page in SharePoint Online?
If you want to change master page in SharePoint Online, follow these steps:
  • Login to your SharePoint Online site >> Click on Settings Gear Icon >> Site Settings
  • From the Site Settings page >> Click On "Master page" link under Look and Feel group. This page lists all master pages available in your SharePoint Online site. (URL shortcut: /_Layouts/ChangeSiteMasterPage.aspx) 
  • Pick the master page to be used on all site pages. Optionally you can specify if you want to "Reset all subsites to inherit this site master page setting" hit OK.
    sharepoint online change master page powershell
  • This sets the master page for your SharePoint online site. From this page, you can set theme and alternate CSS for your site to add even more branding to your SharePoint Online site.
Note: If the Master Page link is not available in site settings, you need to activate SharePoint Publishing feature with these steps: How to Enable Publishing Feature in SharePoint Online?

Now, To automate the process of changing custom Master Page, let use PowerShell!

SharePoint Online: Change Master Page using PowerShell
Here is the SharePoint Online PowerShell to set master page, assuming the custom master page "Crescent.master" is already deployed to the site.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Set-CustomMasterPage()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $MasterPage    
    )

    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the Web
        $Web = $Ctx.web
        $Ctx.Load($Web)
        $Ctx.ExecuteQuery()
    
        #Frame Master page URL
        $MasterPageURL = "$($web.ServerRelativeUrl)/_catalogs/masterpage/$($MasterPage)"

        #Set Default Master page & Custom Master page
        $web.CustomMasterUrl = $MasterPageURL
        $Web.MasterUrl = $MasterPageURL
        $web.Update()
        $Ctx.ExecuteQuery()

        Write-Host "Master Page '$MasterPage' Applied on site '$SiteURL'" -f Green
    }
    Catch {
        write-host -f Red "Error Changing Master Page!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/sales/us"
$MasterPage="Crescent.master"

#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Call the function to change master page
Set-CustomMasterPage -SiteURL $SiteURL -MasterPage $MasterPage
This script sets master page for a given site (web). Lets alter the script slightly to change master page for all subsites in a site collection.

SharePoint Online PowerShell to Set Master Page
To change master page in SharePoint Online site collection including each subsite, use this script.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Set-CustomMasterPage()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $MasterPage    
    )

    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the Web
        $Web = $Ctx.web
        $Ctx.Load($Web)
        $Ctx.Load($Web.Webs)
        $Ctx.ExecuteQuery()
    
        #Frame Master page URL
        $MasterPageURL = "$($web.ServerRelativeUrl)/_catalogs/masterpage/$($MasterPage)"

        #Set Default Master page & Custom Master page
        $web.CustomMasterUrl = $MasterPageURL
        $Web.MasterUrl = $MasterPageURL
        $web.Update()
        $Ctx.ExecuteQuery()

        Write-Host "Master Page '$MasterPage' Applied on site '$SiteURL'" -f Green

        #Call the function recursively for all subsites
        Foreach ($Subsite in $Web.Webs)
        {
            #Call the function to apply master page in sharepoint online
            Set-CustomMasterPage -SiteURL $Subsite.URL -MasterPage $MasterPage
        }
    }
    Catch {
        write-host -f Red "Error Changing Master Page!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/sales/"
$MasterPage="seattle.master"

#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Call the function to change master page
Set-CustomMasterPage -SiteURL $SiteURL -MasterPage $MasterPage
Please note, your custom master page must be uploaded already, before changing the master page (which is obvious!). To upload custom master page to SharePoint online, use : How to upload Master page to SharePoint Online using PowerShell
SharePoint Online: Change Master Page using PowerShell SharePoint Online: Change Master Page using PowerShell Reviewed by Salaudeen Rajack on August 22, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.