add the calender webpart on SharePoint Online by Powershell


/ Published in: Other
Save to your folder(s)

Code to add the calender webpart on SharePoint Online by Powershell


Copy this code and paste it in your HTML
  1. function AddCalenderWebPartToPage ($URL) {
  2. $pageRelativeUrl = "/Pages/Test.aspx"
  3. $wpZoneID = "TopRightRow"$wpZoneOrder= 1
  4.  
  5. $WebPartXml = [xml]'<?xml version="1.0" encoding="utf-8" ?>
  6. < webParts><webPart xmlns="http://schemas.microsoft... name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" /><importErrorMessage>Cannot import this Web Part.</importErrorMessage></metaData><data><properties><property name="ShowWithSampleData" type="bool">False</property><property name="Default" type="string">FALSE</property><property name="NoDefaultStyle" type="string">TRUE</property><property name="CacheXslStorage" type="bool">True</property><property name="ViewContentTypeId" type="string">0x</property><property name="XmlDefinitionLink" type="string" /><property name="ManualRefresh" type="bool">False</property><property name="ListUrl" type="string">Lists/Calendar</property><property name="ListId" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">00000000-0000-0000-0000-000000000000</property><property name="TitleUrl" type="string" /><property name="EnableOriginalValue" type="bool">False</property><property name="Direction" type="direction">NotSet</property><property name="ServerRender" type="bool">False</property><property name="ViewFlags" type="Microsoft.SharePoint.SPViewFlags, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">None</property><property name="AllowConnect" type="bool">True</property><property name="ListName" type="string" null="true" /><property name="ListDisplayName" type="string" null="true" /><property name="AllowZoneChange" type="bool">True</property><property name="ChromeState" type="chromestate">Normal</property><property name="DisableSaveAsNewViewButton" type="bool">False</property><property name="ViewFlag" type="string" /><property name="DataSourceID" type="string" /><property name="ExportMode" type="exportmode">All</property><property name="AutoRefresh" type="bool">False</property><property name="FireInitialRow" type="bool">True</property><property name="AllowEdit" type="bool">True</property><property name="Description" type="string" /><property name="HelpMode" type="helpmode">Modeless</property><property name="BaseXsltHashKey" type="string" null="true" /><property name="AllowMinimize" type="bool">True</property><property name="CacheXslTimeOut" type="int">86400</property><property name="ChromeType" type="chrometype">Default</property><property name="Xsl" type="string" null="true" /><property name="JSLink" type="string" null="true" /><property name="CatalogIconImageUrl" type="string" /><property name="SampleData" type="string" null="true" /><property name="UseSQLDataSourcePaging" type="bool">True</property><property name="TitleIconImageUrl" type="string" /><property name="PageSize" type="int">-1</property><property name="ShowTimelineIfAvailable" type="bool">True</property><property name="Width" type="string" /><property name="DataFields" type="string" /><property name="Hidden" type="bool">False</property><property name="Title" type="string" /><property name="PageType" type="Microsoft.SharePoint.PAGETYPE, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">PAGE_NORMALVIEW</property><property name="DataSourcesString" type="string" /><property name="AllowClose" type="bool">True</property><property name="InplaceSearchEnabled" type="bool">True</property><property name="WebId" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">00000000-0000-0000-0000-000000000000</property><property name="Height" type="string" /><property name="GhostedXslLink" type="string">main.xsl</property><property name="DisableViewSelectorMenu" type="bool">False</property><property name="DisplayName" type="string">Calendar</property><property name="IsClientRender" type="bool">False</property><property name="XmlDefinition" type="string">&lt;View Name="{A32B61DF-039B-4E70-9082-0467924A44E3}" MobileView="TRUE" Type="Calender" Hidden="TRUE" TabularView="TRUE" RecurrenceRowset="TRUE" DisplayName="" Url="/sites/WorkFlowTest7AfterHqDo/Pages/Test.aspx" Level="255" BaseViewID="2" ContentTypeID="0x" MobileUrl="_layouts/15/mobile/viewdaily.aspx" ImageUrl="/_layouts/15/images/events.png?rev=43"&gt;&lt;Toolbar Type="Standard" /&gt;&lt;ViewHeader /&gt;&lt;ViewBody /&gt;&lt;ViewFooter /&gt;&lt;ViewEmpty /&gt;&lt;ParameterBindings&gt;&lt;ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" /&gt;&lt;ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_DEFAULT)" /&gt;&lt;/ParameterBindings&gt;&lt;ViewFields&gt;&lt;FieldRef Name="EventDate" /&gt;&lt;FieldRef Name="EndDate" /&gt;&lt;FieldRef Name="fRecurrence" /&gt;&lt;FieldRef Name="EventType" /&gt;&lt;FieldRef Name="WorkspaceLink" /&gt;&lt;FieldRef Name="Title" /&gt;&lt;FieldRef Name="Location" /&gt;&lt;FieldRef Name="Description" /&gt;&lt;FieldRef Name="Workspace" /&gt;&lt;FieldRef Name="MasterSeriesItemID" /&gt;&lt;FieldRef Name="fAllDayEvent" /&gt;&lt;/ViewFields&gt;&lt;ViewData&gt;&lt;FieldRef Name="Title" Type="CalendarMonthTitle" /&gt;&lt;FieldRef Name="Title" Type="CalendarWeekTitle" /&gt;&lt;FieldRef Name="Location" Type="CalendarWeekLocation" /&gt;&lt;FieldRef Name="Title" Type="CalendarDayTitle" /&gt;&lt;FieldRef Name="Location" Type="CalendarDayLocation" /&gt;&lt;/ViewData&gt;&lt;Query&gt;&lt;Where&gt;&lt;DateRangesOverlap&gt;&lt;FieldRef Name="EventDate" /&gt;&lt;FieldRef Name="EndDate" /&gt;&lt;FieldRef Name="RecurrenceID" /&gt;&lt;Value Type="DateTime"&gt;&lt;Month /&gt;&lt;/Value&gt;&lt;/DateRangesOverlap&gt;&lt;/Where&gt;&lt;/Query&gt;&lt;/View&gt;</property><property name="InitialAsyncDataFetch" type="bool">False</property><property name="AllowHide" type="bool">True</property><property name="ParameterBindings" type="string">&lt;ParameterBinding Name="dvt_sortdir" Location="Postback;Connection"/&gt;&lt;ParameterBinding Name="dvt_sortfield" Location="Postback;Connection"/&gt;&lt;ParameterBinding Name="dvt_startposition" Location="Postback" DefaultValue=""/&gt;&lt;ParameterBinding Name="dvt_firstrow" Location="Postback;Connection"/&gt;&lt;ParameterBinding Name="OpenMenuKeyAccessible" Location="Resource(wss,OpenMenuKeyAccessible)" /&gt;&lt;ParameterBinding Name="open_menu" Location="Resource(wss,open_menu)" /&gt;&lt;ParameterBinding Name="select_deselect_all" Location="Resource(wss,select_deselect_all)" /&gt;&lt;ParameterBinding Name="idPresEnabled" Location="Resource(wss,idPresEnabled)" /&gt;&lt;ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" /&gt;&lt;ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(core,noXinviewofY_DEFAULT)" /&gt;&lt;ParameterBinding Name="AddNewAnnouncement" Location="Resource(wss,addnewitem)" /&gt;&lt;ParameterBinding Name="MoreAnnouncements" Location="Resource(wss,moreItemsParen)" /&gt;</property><property name="DataSourceMode" type="Microsoft.SharePoint.WebControls.SPDataSourceMode, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">List</property><property name="AutoRefreshInterval" type="int">60</property><property name="AsyncRefresh" type="bool">False</property><property name="HelpUrl" type="string" /><property name="MissingAssembly" type="string">Cannot import this Web Part.</property><property name="XslLink" type="string" null="true" /><property name="SelectParameters" type="string" /><property name="HasClientDataSource" type="bool">False</property></properties></data></webPart></webParts>'
  7.  
  8. try{
  9. Write-Host "Starting the Process to add the Calender WebPart to the Home Page" -ForegroundColor Yellow
  10. #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,
  11. #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 Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  12. Write-Host "Getting the page with the webpart we are going to modify" -ForegroundColor Green
  13. $web = $ctx.Web
  14. $ctx.Load($web)
  15. $ctx.ExecuteQuery()
  16. #Using the params, build the page url
  17. $RelURl=$web.ServerRelativeUrl
  18. $pageUrl = $RelURl + $pageRelativeUrl
  19. Write-Host "Getting the page with the webpart we are going to modify: " $pageUrl -ForegroundColor Green
  20. #Getting the page using the GetFileByServerRelativeURL and do the Checkout
  21. #After that, we need to call the executeQuery to do the actions in the site
  22. $page = $ctx.Web.GetFileByServerRelativeUrl($pageUrl);
  23. $page.CheckOut()$ctx.ExecuteQuery()
  24. try{
  25. #Get the webpart manager from the page, to handle the webpartsWrite-Host "The page is checkout" -ForegroundColor Green$webpartManager = $page.GetLimitedWebPartManager([Microsoft.Sharepoint.Client.WebParts.PersonalizationScope]::Shared);
  26. Write-Host $WebPartXml.OuterXml
  27. #Load and execute the query to get the data in the webpartsWrite-Host "Getting the webparts from the page" -ForegroundColor Green$ctx.Load($webpartManager);
  28. $ctx.ExecuteQuery();
  29. #Import the webpart
  30. $wp = $webpartManager.ImportWebPart($WebPartXml.OuterXml)
  31. #Add the webpart to the pageWrite-Host "Add the webpart to the Page" -ForegroundColor Green
  32. $webPartToAdd = $webpartManager.AddWebPart($wp.WebPart, $wpZoneID, $wpZoneOrder) $ctx.Load($webPartToAdd);
  33. $ctx.ExecuteQuery()}
  34. catch{Write-Host "Errors found:`n$_" -ForegroundColor Red
  35. }
  36. finally{#CheckIn and Publish the PageWrite-Host "Checkin and Publish the Page" -ForegroundColor Green
  37. $page.CheckIn("Add the User Profile WebPart", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
  38. $page.Publish("Add the User Profile WebPart")$ctx.ExecuteQuery()
  39. Write-Host "The Calender webpart has been added" -ForegroundColor Yellow }
  40. }catch{Write-Host "Errors found:`n$_" -ForegroundColor Red}
  41. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.