Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using (clientContext)
- {
- Microsoft.SharePoint.Client.File page = clientContext.Web.GetFileByServerRelativeUrl(pageUrl);
- clientContext.Load(page);
- clientContext.ExecuteQuery();
- if (page.CheckOutType == CheckOutType.None)
- {
- page.CheckOut();
- }
- var wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
- clientContext.Load(wpm.WebParts);
- clientContext.ExecuteQuery();
- if (wpm.WebParts.Count != 0)
- {
- foreach (WebPartDefinition webpart in wpm.WebParts)
- {
- webpart.DeleteWebPart();
- clientContext.ExecuteQuery();
- }
- }
- var importedWebPart = wpm.ImportWebPart(webPartSchemaXml);
- var webPart = wpm.AddWebPart(importedWebPart.WebPart, zoneid, zoneIndex.ToInt32());
- page.CheckIn("", CheckinType.MajorCheckIn);
- clientContext.ExecuteQuery();
- }
- function ChangePageLayout()
- {
- param(
- [Parameter(Mandatory=$true)][string]$siteurl,
- [Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
- [Parameter(Mandatory=$false)][string]$PageName,
- [Parameter(Mandatory=$false)][string]$PageLayoutName,
- [Parameter(Mandatory=$false)][string]$PageLayoutDisplayName,
- [Parameter(Mandatory=$false)][string]$Title,
- [Parameter(Mandatory=$false)][bool]$isCustomPageLayout
- )
- try
- {
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)
- $ctx.Credentials = $credentials
- if($isCustomPageLayout -eq $false)
- {
- $PageLayoutName = "/_catalogs/masterpage/" + $PageLayoutName + "," + $PageLayoutDisplayName
- }
- else
- {
- #Here I have assumed that if its custom page layout, then it's placed inside some folder which is child to masterpage
- #If that's not the case with you then you can use below line of code
- #$PageLayoutName = "/_catalogs/masterpage/" + $PageLayoutName + ", " + $PageLayoutDisplayName
- #
- $PageLayoutName = "/_catalogs/masterpage/Custom Page Layouts/" + $PageLayoutName + ", " + $PageLayoutDisplayName
- }
- $Pages = $ctx.Web.Lists.GetByTitle('Pages')
- $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
- $camlQuery.ViewXml = '<View><Query><Where><Eq><FieldRef Name="FileLeafRef" /><Value Type="Text">'+ $PageName +'</Value></Eq></Where></Query></View>'
- $Page = $Pages.GetItems($camlQuery)
- $ctx.Load($Page)
- $ctx.ExecuteQuery()
- $file = $Page.File
- $ctx.Load($file)
- $ctx.ExecuteQuery()
- if ($file.CheckOutType -ne [Microsoft.SharePoint.Client.CheckOutType]::None) {
- $file.UndoCheckOut()
- $ctx.Load($file)
- $ctx.ExecuteQuery()
- }
- $file.CheckOut()
- $ctx.Load($file)
- $ctx.ExecuteQuery()
- $Page.Set_Item("PublishingPageLayout", $PageLayoutName)
- $Page.Set_Item("Title", $Title)
- $Page.Update()
- $Page.File.CheckIn("", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
- $Page.File.Publish("")
- #check for approval
- $ctx.Load($Pages)
- $ctx.ExecuteQuery()
- if ($Pages.EnableModeration -eq $true) {
- $Page.File.Approve("")
- }
- $ctx.Load($Page)
- $ctx.ExecuteQuery()
- Write-Host "Update Page Layout Complete"
- Write-Host ""
- }
- catch
- {
- Write-Host ("Error while changing page layout. Error -->> " + $_.Exception.Message) -ForegroundColor Red
- }
- }
- $credentials = Get-Credential
- ChangePageLayout "http:yoursite.com" $credentials "default.aspx" "YourCustomLayout.aspx" "Your Custom Page Layout" "Some Title" $true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement