Upload and Publish MasterPage to SharePoint Online by using PowerShell/CSOM
cls
#Specify tenant admin and site URL
$User = "Naga@contoso.com"
$SiteURL = "https://*****.sharepoint.com/Subsite"
$MasterPageDeploymentFolder = $("C:\MasterPage")
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
if (!$Context.ServerObjectIsNull.Value)
{
Write-Host "Connected to SharePoint Online site: '$SiteURL'" -ForegroundColor Green
}
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
function Upload-Files()
{
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
# Get the SharePoint web
$web = $Context.Web;
$usrRDBC = $null
#Upload page layouts file
Foreach ($MSFile in (dir $MasterPageDeploymentFolder -File))
{
Write-Host $MSFile.FullName
Deploy-MasterPage -Web $Context.Web -FilePath $MSFile.FullName
}
}
Function Deploy-MasterPage([Microsoft.SharePoint.Client.Web]$Web,[string]$FilePath)
{
$htmlMasterPageContentTypeId = "0x0101000F1C8B9E0EB4BE489F09807B2C53288F0054AD6EF48B9F7B45A142F8173F171BD10003D357F861E29844953D5CAA1D4D8A3A00F416DBD8A79A6643A8465E6D4B8B558A"
$fileName = [System.IO.Path]::GetFileName($FilePath)
$fileContent = [System.IO.File]::ReadAllBytes($FilePath)
$fileInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$fileInfo.Content = $fileContent
$fileInfo.Url = $catalogList.RootFolder.ServerRelativeUrl + "/" + $fileName
$fileInfo.Overwrite = $true
$file = $catalogList.RootFolder.Files.Add($fileInfo)
$Web.Context.Load($file)
$Web.Context.ExecuteQuery()
$listItem = $file.ListItemAllFields
$listItem["Title"] = $file.Name;
#listItem["MasterPageDescription"] = description;
$listItem["ContentTypeId"] = $htmlMasterPageContentTypeId
$listItem["UIVersion"] = [Convert]::ToString(15)
$listItem["PublishingHidden"] = $false
$listItem["HtmlDesignAssociated"] = $true
$listItem.Update()
$listItem.File.Publish("")
$Web.Context.ExecuteQuery()
}
#Execute the function
Upload-Files
#Specify tenant admin and site URL
$User = "Naga@contoso.com"
$SiteURL = "https://*****.sharepoint.com/Subsite"
$MasterPageDeploymentFolder = $("C:\MasterPage")
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
if (!$Context.ServerObjectIsNull.Value)
{
Write-Host "Connected to SharePoint Online site: '$SiteURL'" -ForegroundColor Green
}
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
function Upload-Files()
{
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
# Get the SharePoint web
$web = $Context.Web;
$usrRDBC = $null
#Upload page layouts file
Foreach ($MSFile in (dir $MasterPageDeploymentFolder -File))
{
Write-Host $MSFile.FullName
Deploy-MasterPage -Web $Context.Web -FilePath $MSFile.FullName
}
}
Function Deploy-MasterPage([Microsoft.SharePoint.Client.Web]$Web,[string]$FilePath)
{
$htmlMasterPageContentTypeId = "0x0101000F1C8B9E0EB4BE489F09807B2C53288F0054AD6EF48B9F7B45A142F8173F171BD10003D357F861E29844953D5CAA1D4D8A3A00F416DBD8A79A6643A8465E6D4B8B558A"
$fileName = [System.IO.Path]::GetFileName($FilePath)
$fileContent = [System.IO.File]::ReadAllBytes($FilePath)
$fileInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$fileInfo.Content = $fileContent
$fileInfo.Url = $catalogList.RootFolder.ServerRelativeUrl + "/" + $fileName
$fileInfo.Overwrite = $true
$file = $catalogList.RootFolder.Files.Add($fileInfo)
$Web.Context.Load($file)
$Web.Context.ExecuteQuery()
$listItem = $file.ListItemAllFields
$listItem["Title"] = $file.Name;
#listItem["MasterPageDescription"] = description;
$listItem["ContentTypeId"] = $htmlMasterPageContentTypeId
$listItem["UIVersion"] = [Convert]::ToString(15)
$listItem["PublishingHidden"] = $false
$listItem["HtmlDesignAssociated"] = $true
$listItem.Update()
$listItem.File.Publish("")
$Web.Context.ExecuteQuery()
}
#Execute the function
Upload-Files
No comments:
Post a Comment