8

I want to know how to provision a page layout using PowerShell (CSOM) for Sharepoint Online?

Any information,examples etc are welcome.

I know this can be done using SharePoint hosted App as well. I want to know the best practice to do this.

5

Option 1

How to add/upload publishing page layout via CSOM in PowerShell:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")


Function Get-ClientContext([string]$Url,[string]$UserName,[string]$Password)
{
    $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
    $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
    return $context
}



Function Deploy-PageLayout([Microsoft.SharePoint.Client.Web]$Web,[string]$FilePath,[string]$AssociatedContentTypeId)
{
     $pageLayoutContentTypeId = "0x01010007FF3E057FA8AB4AA42FCB67B453FFC100E214EEE741181F4E9F7ACC43278EE8110003D357F861E29844953D5CAA1D4D8A3B001EC1BD45392B7A458874C52A24C9F70B"
     $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"] = title;
     #listItem["MasterPageDescription"] = description;
     $listItem["ContentTypeId"] = $pageLayoutContentTypeId
     $listItem["PublishingAssociatedContentType"] = [string]::Format(";#{0};#{1};#", $associatedContentType.Name, $associatedContentType.Id.StringValue)
     $listItem["UIVersion"] = [Convert]::ToString(15)
     $listItem.Update()

     $Web.Context.ExecuteQuery()
}


$UserName = "jdoe@contoso.onmicrosoft.com"
$Password = Read-Host -Prompt "Enter the password"    
$Url = "https://contoso.sharepoint.com/"

$WelcomePageContentTypeId = "0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF390064DEA0F50FC8C147B0B6EA0636C4A7D4"
$WelcomePageFilePath = "C:\Assets\ContosoBlankWebPartPage.aspx"


$context = Get-ClientContext -Url $Url -UserName $UserName -Password $Password
Deploy-PageLayout -Web $context.Web -FilePath $WelcomePageFilePath -AssociatedContentTypeId $WelcomePageContentTypeId
$context.Dispose()

Option 2

You could utilize PowerShell cmdlets from PnP project, in particular Add-SPOPublishingPageLayout command.

  • I have tried executing the above script. Included my server details,credentials and modified the path of layout to my local path. I am getting error Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (401) Unauthorized." – SPD May 5 '15 at 5:12
0

I searched for you query on Google and found a link to the code-project site pointing on the same problem as yours.

you will first need to import the Microsoft.SharePoint.Publishing.dll into power-shell and then follow the steps provided in this link

  • 1
    I fail to see how your details in this answer is at all related to the question? – Robert Lindgren May 4 '15 at 6:15
  • Thanks for the response. I understand how to use the cmdlets. I just want to know if there is a way to upload a page layout to Gallery and associate a Content Type to it using Powershell programatically. – SPD May 4 '15 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.