Upload and Publish page layouts to SharePoint Online by using PowerShell/CSOM
cls#Specify tenant admin and site URL
$User = "Naga@contoso.com"
$SiteURL = "https://*****.sharepoint.com/Subsite"
$PageLayoutDeploymentFolder = $("C:\PageLayouts")
#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
$ArticlePageContentTypeId = "0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D"
#Upload page layouts file
Foreach ($PLFile in (dir $PageLayoutDeploymentFolder -File))
{
Write-Host $PLFile.FullName
Deploy-PageLayout -Web $Context.Web -FilePath $PLFile.FullName -AssociatedContentTypeId $ArticlePageContentTypeId
}
}
Function Deploy-PageLayout([Microsoft.SharePoint.Client.Web]$Web,[string]$FilePath,[string]$AssociatedContentTypeId)
{
$pageLayoutContentTypeId = "0x01010007FF3E057FA8AB4AA42FCB67B453FFC100E214EEE741181F4E9F7ACC43278EE811002985A1BA00CCBC45B088E4150D7BC86C"
$fileName = [System.IO.Path]::GetFileName($FilePath)
$associatedContentType = $Web.AvailableContentTypes.GetById($AssociatedContentTypeId)
$catalogList = $Web.GetCatalog([Microsoft.SharePoint.Client.ListTemplateType]::MasterPageCatalog)
$Web.Context.Load($catalogList.RootFolder)
$Web.Context.Load($associatedContentType)
$Web.Context.ExecuteQuery()
$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"] = $pageLayoutContentTypeId
$listItem["PublishingAssociatedContentType"] = [string]::Format(";#{0};#{1};#", $associatedContentType.Name, $associatedContentType.Id.StringValue)
$listItem["UIVersion"] = [Convert]::ToString(15)
$listItem["PublishingHidden"] = $false
$listItem.Update()
$listItem.File.Publish("")
$Web.Context.ExecuteQuery()
}
#Execute the function
Upload-Files
Happy coding.. :)
No comments:
Post a Comment