Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try{
- Add-Type -Path 'C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll'
- Add-Type -Path 'C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll'
- Add-Type -Path 'C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Publishing.dll'
- Add-Type -Path 'C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Taxonomy.dll'
- }
- catch {
- Throw "Unable to load SharePoint Client runtime"
- }
- $URL="https://netapp.sharepoint.com/sites/AshokTestJive"
- $pUsername="pshagadm@netapp.com"
- $pPassword = "K3xr7PN6"
- $HostName = "https://netapp.sharepoint.com"
- $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($pUsername, $(convertto-securestring $pPassword -asplaintext -force))
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($URL)
- $ctx.Credentials = $Creds
- function AddCalenderWebPartToPage ($URL) {
- $pageRelativeUrl = "/Pages/Home.aspx"
- $wpZoneID = "TopRightRow"
- $wpZoneOrder= 1
- $WebPartXml = [xml]''
- try{
- Write-Host "Starting the Process to add the Calender WebPart to the Home Page" -ForegroundColor Yellow
- #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,
- #but we could execute it from wherever we want, only need to copy the dlls and reference the path from here
- Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
- Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
- Write-Host "Getting the page with the webpart we are going to modify" -ForegroundColor Green
- $web = $ctx.Web
- $ctx.Load($web)
- $ctx.ExecuteQuery()
- #Using the params, build the page url
- $RelURl=$web.ServerRelativeUrl
- $pageUrl = $RelURl + $pageRelativeUrl
- Write-Host "Getting the page with the webpart we are going to modify: " $pageUrl -ForegroundColor Green
- #Getting the page using the GetFileByServerRelativeURL and do the Checkout
- #After that, we need to call the executeQuery to do the actions in the site
- $page = $ctx.Web.GetFileByServerRelativeUrl($pageUrl);
- $page.CheckOut()
- $ctx.ExecuteQuery()
- try{
- #Get the webpart manager from the page, to handle the webparts
- Write-Host "The page is checkout" -ForegroundColor Green
- $webpartManager = $page.GetLimitedWebPartManager([Microsoft.Sharepoint.Client.WebParts.PersonalizationScope]::Shared);
- Write-Host $WebPartXml.OuterXml
- #Load and execute the query to get the data in the webparts
- Write-Host "Getting the webparts from the page" -ForegroundColor Green
- $ctx.Load($webpartManager);
- $ctx.ExecuteQuery();
- #Import the webpart
- $wp = $webpartManager.ImportWebPart($WebPartXml.OuterXml)
- #Add the webpart to the page
- Write-Host "Add the webpart to the Page" -ForegroundColor Green
- $webPartToAdd = $webpartManager.AddWebPart($wp.WebPart, $wpZoneID, $wpZoneOrder)
- $ctx.Load($webPartToAdd);
- $ctx.ExecuteQuery()
- }
- catch{
- Write-Host "Errors found:`n$_" -ForegroundColor Red
- }
- finally{
- #CheckIn and Publish the Page
- Write-Host "Checkin and Publish the Page" -ForegroundColor Green
- $page.CheckIn("Add the User Profile WebPart", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
- $page.Publish("Add the User Profile WebPart")
- $ctx.ExecuteQuery()
- Write-Host "The Calender webpart has been added" -ForegroundColor Yellow
- }
- }
- catch{
- Write-Host "Errors found:`n$_" -ForegroundColor Red
- }
- }
- AddCalenderWebPartToPage ($URL)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement