Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. try{
  2. Add-Type -Path 'C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll'
  3. Add-Type -Path 'C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll'
  4. Add-Type -Path 'C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Publishing.dll'
  5. Add-Type -Path 'C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Taxonomy.dll'
  6. }
  7.  
  8. catch {
  9. Throw "Unable to load SharePoint Client runtime"
  10. }
  11.  
  12. $URL="https://netapp.sharepoint.com/sites/AshokTestJive"
  13.  
  14. $pUsername="pshagadm@netapp.com"
  15. $pPassword = "K3xr7PN6"
  16. $HostName = "https://netapp.sharepoint.com"
  17. $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($pUsername, $(convertto-securestring $pPassword -asplaintext -force))
  18.  
  19.  
  20. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($URL)
  21. $ctx.Credentials = $Creds
  22.  
  23. function AddCalenderWebPartToPage ($URL) {
  24. $pageRelativeUrl = "/Pages/Home.aspx"
  25. $wpZoneID = "TopRightRow"
  26. $wpZoneOrder= 1
  27.  
  28. $WebPartXml = [xml]''
  29.  
  30. try{
  31.  
  32. Write-Host "Starting the Process to add the Calender WebPart to the Home Page" -ForegroundColor Yellow
  33.  
  34. #Adding the reference to the client libraries. Here I'm executing this for a SharePoint Server (and I'm referencing it from the SharePoint ISAPI directory,
  35. #but we could execute it from wherever we want, only need to copy the dlls and reference the path from here
  36. Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
  37. Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
  38.  
  39. Write-Host "Getting the page with the webpart we are going to modify" -ForegroundColor Green
  40. $web = $ctx.Web
  41. $ctx.Load($web)
  42. $ctx.ExecuteQuery()
  43. #Using the params, build the page url
  44. $RelURl=$web.ServerRelativeUrl
  45. $pageUrl = $RelURl + $pageRelativeUrl
  46. Write-Host "Getting the page with the webpart we are going to modify: " $pageUrl -ForegroundColor Green
  47.  
  48. #Getting the page using the GetFileByServerRelativeURL and do the Checkout
  49. #After that, we need to call the executeQuery to do the actions in the site
  50. $page = $ctx.Web.GetFileByServerRelativeUrl($pageUrl);
  51. $page.CheckOut()
  52. $ctx.ExecuteQuery()
  53. try{
  54.  
  55. #Get the webpart manager from the page, to handle the webparts
  56. Write-Host "The page is checkout" -ForegroundColor Green
  57. $webpartManager = $page.GetLimitedWebPartManager([Microsoft.Sharepoint.Client.WebParts.PersonalizationScope]::Shared);
  58.  
  59. Write-Host $WebPartXml.OuterXml
  60.  
  61. #Load and execute the query to get the data in the webparts
  62. Write-Host "Getting the webparts from the page" -ForegroundColor Green
  63. $ctx.Load($webpartManager);
  64. $ctx.ExecuteQuery();
  65.  
  66. #Import the webpart
  67. $wp = $webpartManager.ImportWebPart($WebPartXml.OuterXml)
  68.  
  69.  
  70. #Add the webpart to the page
  71. Write-Host "Add the webpart to the Page" -ForegroundColor Green
  72. $webPartToAdd = $webpartManager.AddWebPart($wp.WebPart, $wpZoneID, $wpZoneOrder)
  73.  
  74. $ctx.Load($webPartToAdd);
  75. $ctx.ExecuteQuery()
  76. }
  77. catch{
  78. Write-Host "Errors found:`n$_" -ForegroundColor Red
  79.  
  80. }
  81. finally{
  82. #CheckIn and Publish the Page
  83. Write-Host "Checkin and Publish the Page" -ForegroundColor Green
  84. $page.CheckIn("Add the User Profile WebPart", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
  85. $page.Publish("Add the User Profile WebPart")
  86. $ctx.ExecuteQuery()
  87.  
  88. Write-Host "The Calender webpart has been added" -ForegroundColor Yellow
  89.  
  90. }
  91.  
  92. }
  93. catch{
  94. Write-Host "Errors found:`n$_" -ForegroundColor Red
  95. }
  96.  
  97. }
  98.  
  99. AddCalenderWebPartToPage ($URL)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement