Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
  2. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
  3.  
  4.  
  5. Function Get-ClientContext([string]$Url,[string]$UserName,[string]$Password)
  6. {
  7. $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
  8. $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  9. $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
  10. return $context
  11. }
  12.  
  13.  
  14.  
  15. Function Deploy-PageLayout([Microsoft.SharePoint.Client.Web]$Web,[string]$FilePath,[string]$AssociatedContentTypeId)
  16. {
  17. $pageLayoutContentTypeId = "0x01010007FF3E057FA8AB4AA42FCB67B453FFC100E214EEE741181F4E9F7ACC43278EE8110003D357F861E29844953D5CAA1D4D8A3B001EC1BD45392B7A458874C52A24C9F70B"
  18. $fileName = [System.IO.Path]::GetFileName($FilePath)
  19.  
  20. $associatedContentType = $Web.AvailableContentTypes.GetById($AssociatedContentTypeId)
  21. $catalogList = $Web.GetCatalog([Microsoft.SharePoint.Client.ListTemplateType]::MasterPageCatalog)
  22. $Web.Context.Load($catalogList.RootFolder)
  23. $Web.Context.Load($associatedContentType)
  24. $Web.Context.ExecuteQuery()
  25.  
  26.  
  27.  
  28. $fileContent = [System.IO.File]::ReadAllBytes($FilePath)
  29. $fileInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
  30. $fileInfo.Content = $fileContent
  31. $fileInfo.Url = $catalogList.RootFolder.ServerRelativeUrl + "/" + $fileName
  32. $fileInfo.Overwrite = $true
  33.  
  34. $file = $catalogList.RootFolder.Files.Add($fileInfo)
  35. $Web.Context.Load($file)
  36. $Web.Context.ExecuteQuery()
  37.  
  38. $listItem = $file.ListItemAllFields
  39. #listItem["Title"] = title;
  40. #listItem["MasterPageDescription"] = description;
  41. $listItem["ContentTypeId"] = $pageLayoutContentTypeId
  42. $listItem["PublishingAssociatedContentType"] = [string]::Format(";#{0};#{1};#", $associatedContentType.Name, $associatedContentType.Id.StringValue)
  43. $listItem["UIVersion"] = [Convert]::ToString(15)
  44. $listItem.Update()
  45.  
  46. $Web.Context.ExecuteQuery()
  47. }
  48.  
  49.  
  50. $UserName = "user@tenant.onmicrosoft.com"
  51. $Password = Read-Host -Prompt "Enter the password"
  52. $Url = "https://tenant-name.sharepoint.com/"
  53.  
  54. $WelcomePageContentTypeId = "0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF390064DEA0F50FC8C147B0B6EA0636C4A7D4"
  55. $WelcomePageFilePath = "C:AssetsContosoBlankWebPartPage.aspx" //change path as per your structure
  56.  
  57. $context = Get-ClientContext -Url $Url -UserName $UserName -Password $Password
  58. Deploy-PageLayout -Web $context.Web -FilePath $WelcomePageFilePath -AssociatedContentTypeId $WelcomePageContentTypeId
  59. $context.Dispose()
  60.  
  61. $siteurl = "https://tenant.sharepoint.com"
  62. Connect-SPOnline -Url $siteurl
  63. $ctx = Get-SPOContext
  64.  
  65. $pageCT = Get-SPOContentType -Identity "Article Page"
  66. Write-Host $pageCT.Id
  67. $pageLayout = Add-SPOPublishingPageLayout -SourceFilePath "C:AssetsTestLayout.aspx" -Title "TestLayout" -Description "Page Layout for testing PnP scripts" -DestinationFolderHierarchy "/" -AssociatedContentTypeID $pageCT.Id
  68.  
  69. $siteurl = "https://tenant.sharepoint.com"
  70. Connect-PnPOnline -Url $siteurl
  71. $ctx = Get-SPOContext
  72.  
  73. $pageCT = Get-PnPContentType -Identity "Article Page"
  74. Write-Host $pageCT.Id
  75. $pageLayout = Add-PnPPublishingPageLayout -SourceFilePath "C:AssetsTestLayout.aspx" -Title "TestLayout" -Description "Page Layout for testing PnP scripts" -DestinationFolderHierarchy "/" -AssociatedContentTypeID $pageCT.Id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement