Adding a web part page to Office 365 team site library using CSOM + PowerShell

Hi All,

Next step towards Office 365 development is uploading /adding a web part page in “SitePages” library of team site. The site is not publishing site so we couldn’t like upload the empty page and then apply PageLayout.

So we will require a local copy of page having appropriate zones. Example of page (default.aspx) structure (this page is just for reference):



Following are the steps and script:

  1. Get the Microsoft.SharePoint.Client.ClientContext instance
  2. Get the Web instance
  3. Get the “Site Pages” library
  4. Get the root folder and files collection
  5. For adding file, we need to create instance of Microsoft.SharePoint.Client.FileCreationInformation. Set the properties of this instance like FileUrl, FileContent etc.
  6. Use the Add () method of files collection.

Script

  1. #Variables which we are used below  
  2. $url = http://MyWeb.com  
  3.   
  4. $LayoutPath = “c:\default.aspx”  
  5.   
  6. $PagesLibraryTitle = “Site Pages”  
  7.   
  8. $FileNameWithExtention = “default.aspx”  
  9. # All the above variables can be parameterised  
  10.  
  11.  #Password to connect the Office 365 site  
  12.   
  13. $securePassword = ConvertTo-SecureString -String $Password -AsPlainText –Force  
  14.  
  15. #instance of Client Context  
  16.   
  17. [Microsoft.SharePoint.Client.ClientContext]$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)  
  18.   
  19. $clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $securePassword)  
  20.  
  21. #here assuming we are adding file to root web of site collection  
  22. $site = $clientContext.Site  
  23. $web = $site.RootWeb  
  24.   
  25. $clientContext.Load($web)  
  26. $clientContext.ExecuteQuery()  
  27.  
  28. #List Collection  
  29. $lists = $web.Lists  
  30.   
  31. $clientContext.Load($lists)  
  32. $clientContext.ExecuteQuery()  
  33.  
  34. #Get the list  
  35. $list = $lists.GetByTitle($PagesLibraryTitle)  
  36.   
  37. $clientContext.Load($list)  
  38. $clientContext.ExecuteQuery()  
  39.  
  40. #Files Collection  
  41. $files = $list.RootFolder.Files  
  42.   
  43. $clientContext.Load($files)  
  44. $clientContext.ExecuteQuery()  
  45.  
  46. #instance of FileCreationInformation  
  47. [Microsoft.SharePoint.Client.FileCreationInformation]$fileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation  
  48.  
  49. #Get the file content  
  50. $fileContent = Get-Content $LayoutPath  
  51.  
  52. #convert the file content into Byte[] and set it to Content property of FileCreationInfo class  
  53. $fileCreationInfo.Content = [System.Text.Encoding]::UTF8.GetBytes($fileContent)  
  54.   
  55. $fileCreationInfo.Url = $FileNameWithExtention #(default.aspx)  
  56.  
  57. #add the file  
  58. $page = $files.Add($fileCreationInfo)  
  59. $clientContext.ExecuteQuery()  
Thanks

Feel free to comment / feedback / suggestions / thoughts if any or if you have any query