/ Published in: Other
Code to add the calender webpart on SharePoint Online by Powershell
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function AddCalenderWebPartToPage ($URL) { $pageRelativeUrl = "/Pages/Test.aspx" $wpZoneID = "TopRightRow"$wpZoneOrder= 1 $WebPartXml = [xml]'<?xml version="1.0" encoding="utf-8" ?> < 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"><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"><Toolbar Type="Standard" /><ViewHeader /><ViewBody /><ViewFooter /><ViewEmpty /><ParameterBindings><ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" /><ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_DEFAULT)" /></ParameterBindings><ViewFields><FieldRef Name="EventDate" /><FieldRef Name="EndDate" /><FieldRef Name="fRecurrence" /><FieldRef Name="EventType" /><FieldRef Name="WorkspaceLink" /><FieldRef Name="Title" /><FieldRef Name="Location" /><FieldRef Name="Description" /><FieldRef Name="Workspace" /><FieldRef Name="MasterSeriesItemID" /><FieldRef Name="fAllDayEvent" /></ViewFields><ViewData><FieldRef Name="Title" Type="CalendarMonthTitle" /><FieldRef Name="Title" Type="CalendarWeekTitle" /><FieldRef Name="Location" Type="CalendarWeekLocation" /><FieldRef Name="Title" Type="CalendarDayTitle" /><FieldRef Name="Location" Type="CalendarDayLocation" /></ViewData><Query><Where><DateRangesOverlap><FieldRef Name="EventDate" /><FieldRef Name="EndDate" /><FieldRef Name="RecurrenceID" /><Value Type="DateTime"><Month /></Value></DateRangesOverlap></Where></Query></View></property><property name="InitialAsyncDataFetch" type="bool">False</property><property name="AllowHide" type="bool">True</property><property name="ParameterBindings" type="string"><ParameterBinding Name="dvt_sortdir" Location="Postback;Connection"/><ParameterBinding Name="dvt_sortfield" Location="Postback;Connection"/><ParameterBinding Name="dvt_startposition" Location="Postback" DefaultValue=""/><ParameterBinding Name="dvt_firstrow" Location="Postback;Connection"/><ParameterBinding Name="OpenMenuKeyAccessible" Location="Resource(wss,OpenMenuKeyAccessible)" /><ParameterBinding Name="open_menu" Location="Resource(wss,open_menu)" /><ParameterBinding Name="select_deselect_all" Location="Resource(wss,select_deselect_all)" /><ParameterBinding Name="idPresEnabled" Location="Resource(wss,idPresEnabled)" /><ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" /><ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(core,noXinviewofY_DEFAULT)" /><ParameterBinding Name="AddNewAnnouncement" Location="Resource(wss,addnewitem)" /><ParameterBinding Name="MoreAnnouncements" Location="Resource(wss,moreItemsParen)" /></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>' 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 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" 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 webpartsWrite-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 webpartsWrite-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 pageWrite-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 PageWrite-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} }