Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to add a Calender web part on a publishing/Blank Web Part Page on a SharePoint Online site.

I tried adding it through csom but I am not able to set the calendar view as the web part view.

I tried manipulating the XML definition of the web part but failed.

What I have tried:

I tried adding it through csom but I am not able to set the calendar view as the web part view.

I tried manipulating the XML definition of the web part but failed.


#Pls find the code below--
function AddCalenderWebPartToPage ($URL) {


$pageRelativeUrl = "/Pages/Test.aspx"
$wpZoneID = "TopRightRow"
$wpZoneOrder= 1

$WebPartXml = [xml]'

<webparts>
<webpart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
<importerrormessage>Cannot import this Web Part.
</metaData>

<properties>
<property name="ShowWithSampleData" type="bool">False
<property name="Default" type="string">FALSE
<property name="NoDefaultStyle" type="string">TRUE
<property name="CacheXslStorage" type="bool">True
<property name="ViewContentTypeId" type="string">0x
<property name="XmlDefinitionLink" type="string">
<property name="ManualRefresh" type="bool">False
<property name="ListUrl" type="string">Lists/Calendar
<property name="ListId" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">00000000-0000-0000-0000-000000000000
<property name="TitleUrl" type="string">
<property name="EnableOriginalValue" type="bool">False
<property name="Direction" type="direction">NotSet
<property name="ServerRender" type="bool">False
<property name="ViewFlags" type="Microsoft.SharePoint.SPViewFlags, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">None
<property name="AllowConnect" type="bool">True
<property name="ListName" type="string" null="true">
<property name="ListDisplayName" type="string" null="true">
<property name="AllowZoneChange" type="bool">True
<property name="ChromeState" type="chromestate">Normal
<property name="DisableSaveAsNewViewButton" type="bool">False
<property name="ViewFlag" type="string">
<property name="DataSourceID" type="string">
<property name="ExportMode" type="exportmode">All
<property name="AutoRefresh" type="bool">False
<property name="FireInitialRow" type="bool">True
<property name="AllowEdit" type="bool">True
<property name="Description" type="string">
<property name="HelpMode" type="helpmode">Modeless
<property name="BaseXsltHashKey" type="string" null="true">
<property name="AllowMinimize" type="bool">True
<property name="CacheXslTimeOut" type="int">86400
<property name="ChromeType" type="chrometype">Default
<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 name="TitleIconImageUrl" type="string">
<property name="PageSize" type="int">-1
<property name="ShowTimelineIfAvailable" type="bool">True
<property name="Width" type="string">
<property name="DataFields" type="string">
<property name="Hidden" type="bool">False
<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 name="DataSourcesString" type="string">
<property name="AllowClose" type="bool">True
<property name="InplaceSearchEnabled" type="bool">True
<property name="WebId" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">00000000-0000-0000-0000-000000000000
<property name="Height" type="string">
<property name="GhostedXslLink" type="string">main.xsl
<property name="DisableViewSelectorMenu" type="bool">False
<property name="DisplayName" type="string">Calendar
<property name="IsClientRender" type="bool">False
<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 name="InitialAsyncDataFetch" type="bool">False
<property name="AllowHide" type="bool">True
<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 name="DataSourceMode" type="Microsoft.SharePoint.WebControls.SPDataSourceMode, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">List
<property name="AutoRefreshInterval" type="int">60
<property name="AsyncRefresh" type="bool">False
<property name="HelpUrl" type="string">
<property name="MissingAssembly" type="string">Cannot import this Web Part.
<property name="XslLink" type="string" null="true">
<property name="SelectParameters" type="string">
<property name="HasClientDataSource" type="bool">False



'

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 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
}

}
Posted
Updated 21-Jun-16 5:20am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900