kwizcom banner advertisement

SharePoint Online: PowerShell to Upload Master Page

Requirement: Upload master page to SharePoint Online

How to add master page to SharePoint Online?
To add master page to sharepoint online,
  • Login to your SharePoint Online site >> Click on Settings >> Site Settings
  • In the Site Settings page, Click on "Master Pages" under Web Designer Galleries. This library is similar to any other document library. 
    sharepoint online upload master page
  • Click on "Upload Document" link, browse and locate your custom master page and complete uploading. 
    upload master page to sharepoint online
  • Once uploaded, you'll be able to change the master page either from SharePoint web UI or using PowerShell.


PowerShell to Upload Master page to SharePoint Online
This PowerShell script gets the Master page from the location specified, and uploads to SharePoint Online.  
#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 Upload-CustomMasterPage()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $MasterPageLocation
    )

    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
    
        #Get the Web
        $Web = $Ctx.web
        $Ctx.Load($Web)
   
        #Get the Master Page Gallery
        $MasterPageGallery = $Web.Lists.GetByTitle("Master Page Gallery")
        $Ctx.Load($MasterPageGallery)
        $Ctx.ExecuteQuery()

        #Get the Master page from given local location 
        $MasterPageFile = Get-Item $MasterPageLocation
        $MasterPageContent = [System.IO.File]::ReadAllBytes($MasterPageFile.FullName)

        #Prepare the File to upload Master page Gallary
        $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
        $FileCreationInfo.Overwrite = $True
        $FileCreationInfo.Content = $MasterPageContent
        $FileCreationInfo.URL = $MasterPageFile.name
        
        Write-host "Uploading Master Page to Site:"$SiteURL
        #Upload the File to Master page Gallary
        $MasterPage = $MasterPageGallery.RootFolder.Files.Add($FileCreationInfo)  
        $Ctx.ExecuteQuery()

        #Check-Out and Check-In to create major version
        $MasterPage.CheckOut()
        $Ctx.ExecuteQuery()
        $CheckIn = $MasterPage.CheckIn("Master Page Uploaded",[Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
        $Ctx.ExecuteQuery()
        
        Write-Host "Master Page '$MasterPageFile' Uploaded to site '$SiteURL'" -f Green

        <#Process subsites - Call the function recursively
        $Ctx.Load($Web.Webs)
        $Ctx.ExecuteQuery()
        If($Web.Webs.Count -gt 0)
        {
            Foreach ($web in $Web.Webs)
            {
                Upload-CustomMasterPage -SiteURL $web.Url  -MasterPageLocation $MasterPageLocation
            }
        }#>
  }
    Catch {
        write-host -f Red "Error Uploading Custom Master Page!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/sales/"
$MasterPageLocation="C:\Deployment\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
Upload-CustomMasterPage -SiteURL $SiteURL -MasterPageLocation $MasterPageLocation

Please note, this script uploads the master page only to the given site URL. If you want to upload the master page to all subsites underneath, uncommnet the lines . Once the Master page is uploaded, you can use this another article to apply the master page in SharePoint Online: How to Set Custom Master Page in SharePoint Online using PowerShell?
SharePoint Online: PowerShell to Upload Master Page SharePoint Online: PowerShell to Upload Master Page Reviewed by Salaudeen Rajack on 2:05 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.