Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. using (clientContext)
  2. {
  3. Microsoft.SharePoint.Client.File page = clientContext.Web.GetFileByServerRelativeUrl(pageUrl);
  4. clientContext.Load(page);
  5. clientContext.ExecuteQuery();
  6.  
  7. if (page.CheckOutType == CheckOutType.None)
  8. {
  9. page.CheckOut();
  10. }
  11. var wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
  12. clientContext.Load(wpm.WebParts);
  13. clientContext.ExecuteQuery();
  14.  
  15. if (wpm.WebParts.Count != 0)
  16. {
  17. foreach (WebPartDefinition webpart in wpm.WebParts)
  18. {
  19. webpart.DeleteWebPart();
  20. clientContext.ExecuteQuery();
  21. }
  22. }
  23.  
  24. var importedWebPart = wpm.ImportWebPart(webPartSchemaXml);
  25. var webPart = wpm.AddWebPart(importedWebPart.WebPart, zoneid, zoneIndex.ToInt32());
  26. page.CheckIn("", CheckinType.MajorCheckIn);
  27.  
  28. clientContext.ExecuteQuery();
  29.  
  30. }
  31.  
  32. function ChangePageLayout()
  33. {
  34. param(
  35. [Parameter(Mandatory=$true)][string]$siteurl,
  36. [Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
  37. [Parameter(Mandatory=$false)][string]$PageName,
  38. [Parameter(Mandatory=$false)][string]$PageLayoutName,
  39. [Parameter(Mandatory=$false)][string]$PageLayoutDisplayName,
  40. [Parameter(Mandatory=$false)][string]$Title,
  41. [Parameter(Mandatory=$false)][bool]$isCustomPageLayout
  42. )
  43. try
  44. {
  45.  
  46. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)
  47. $ctx.Credentials = $credentials
  48.  
  49. if($isCustomPageLayout -eq $false)
  50. {
  51. $PageLayoutName = "/_catalogs/masterpage/" + $PageLayoutName + "," + $PageLayoutDisplayName
  52. }
  53. else
  54. {
  55. #Here I have assumed that if its custom page layout, then it's placed inside some folder which is child to masterpage
  56. #If that's not the case with you then you can use below line of code
  57. #$PageLayoutName = "/_catalogs/masterpage/" + $PageLayoutName + ", " + $PageLayoutDisplayName
  58. #
  59. $PageLayoutName = "/_catalogs/masterpage/Custom Page Layouts/" + $PageLayoutName + ", " + $PageLayoutDisplayName
  60. }
  61.  
  62. $Pages = $ctx.Web.Lists.GetByTitle('Pages')
  63. $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
  64. $camlQuery.ViewXml = '<View><Query><Where><Eq><FieldRef Name="FileLeafRef" /><Value Type="Text">'+ $PageName +'</Value></Eq></Where></Query></View>'
  65. $Page = $Pages.GetItems($camlQuery)
  66. $ctx.Load($Page)
  67. $ctx.ExecuteQuery()
  68.  
  69. $file = $Page.File
  70.  
  71. $ctx.Load($file)
  72. $ctx.ExecuteQuery()
  73.  
  74. if ($file.CheckOutType -ne [Microsoft.SharePoint.Client.CheckOutType]::None) {
  75. $file.UndoCheckOut()
  76. $ctx.Load($file)
  77. $ctx.ExecuteQuery()
  78. }
  79.  
  80. $file.CheckOut()
  81. $ctx.Load($file)
  82. $ctx.ExecuteQuery()
  83.  
  84. $Page.Set_Item("PublishingPageLayout", $PageLayoutName)
  85. $Page.Set_Item("Title", $Title)
  86. $Page.Update()
  87. $Page.File.CheckIn("", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
  88. $Page.File.Publish("")
  89.  
  90. #check for approval
  91. $ctx.Load($Pages)
  92. $ctx.ExecuteQuery()
  93.  
  94. if ($Pages.EnableModeration -eq $true) {
  95. $Page.File.Approve("")
  96. }
  97.  
  98. $ctx.Load($Page)
  99. $ctx.ExecuteQuery()
  100. Write-Host "Update Page Layout Complete"
  101. Write-Host ""
  102.  
  103. }
  104. catch
  105. {
  106. Write-Host ("Error while changing page layout. Error -->> " + $_.Exception.Message) -ForegroundColor Red
  107. }
  108. }
  109.  
  110. $credentials = Get-Credential
  111. 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