0

I need to create a new page Layout and add it a content type using powershell csom. mean create the aspx and the Html of the page Layout. Any idea.

0

You can try the below code using CSOM API:

Option 1:

[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 = "user@tenant.onmicrosoft.com"
$Password = Read-Host -Prompt "Enter the password"    
$Url = "https://tenant-name.sharepoint.com/"

$WelcomePageContentTypeId = "0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF390064DEA0F50FC8C147B0B6EA0636C4A7D4"
$WelcomePageFilePath = "C:\Assets\ContosoBlankWebPartPage.aspx" //change path as per your structure

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

Reference - upload a new page layout into master page gallery using powershell

Option 2

You can use the PnP powershell as below:

Download the PnP powershell from below link and install it:

SharePoint PnP Powershell releases

After installing, try the code as below:

$siteurl = "https://tenant.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$ctx = Get-SPOContext  

$pageCT = Get-SPOContentType -Identity "Article Page"  
Write-Host $pageCT.Id  
$pageLayout = Add-SPOPublishingPageLayout -SourceFilePath "C:\Assets\TestLayout.aspx" -Title "TestLayout" -Description "Page Layout for testing PnP scripts" -DestinationFolderHierarchy "/" -AssociatedContentTypeID $pageCT.Id

According to latest PnP powershell, the code would be as below:

$siteurl = "https://tenant.sharepoint.com"  
Connect-PnPOnline -Url $siteurl  
$ctx = Get-SPOContext  

$pageCT = Get-PnPContentType -Identity "Article Page"  
Write-Host $pageCT.Id  
$pageLayout = Add-PnPPublishingPageLayout -SourceFilePath "C:\Assets\TestLayout.aspx" -Title "TestLayout" -Description "Page Layout for testing PnP scripts" -DestinationFolderHierarchy "/" -AssociatedContentTypeID $pageCT.Id
0

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.