Search code examples
sharepoint-onlinepower-automatesharepoint-rest-api

Create Modern Calendar view for SharePoint Online List using the REST API


I've been trying to create a modern calendar list in SharePoint using the REST API through Power Automate.

When using the UI, the only way I've seen how to do this is by first creating a modern list, adding Start and End Date fields, then creating the modern calendar view with those fields. (For my purposes all I need are Title, Start Date, and End Date.)

Following that same process through the REST API, I am able to create the list and fields just fine.
However, there doesn't seem to be any documentation on specifying the Start and End date fields for the modern calendar view, so when first opening the view, it gives this nasty popup telling the user to select the Title, Start, and End dates that the view will use: Calendar View first opening popup

The body of my query is:

{ '__metadata': { 'type': 'SP.View'},
'ViewType': 'HTML',
'ViewType2':'MODERNCALENDAR', 
'Title': 'Calendar',
'DefaultView': true
}

From what I've seen when I query for a calendar view that works, the only properties that seem to have anything to do with Start and End Dates are ListViewXml and HtmlSchemaXml, but neither of those explicitly state which fields are being used for the Start and End date in the view.
Correct ListViewXML Example:

<View Name=\"{F0105F06-6621-44AD-9E25-F3D0928D3048}\" Type=\"HTML\" DisplayName=\"Calendar\" Url=\"/sites/Test-1/Lists/Calendar/Calendar.aspx\" Level=\"1\" BaseViewID=\"1\" ContentTypeID=\"0x\" ImageUrl=\"/_layouts/15/images/generic.png?rev=47\" ><Query /><ViewFields><FieldRef Name=\"Start_x0020_Date\" /><FieldRef Name=\"End_x0020_Date\" /><FieldRef Name=\"LinkTitle\" /></ViewFields><RowLimit Paged=\"FALSE\">30</RowLimit><JSLink>clienttemplates.js</JSLink><XslLink Default=\"TRUE\">main.xsl</XslLink><ViewType2>MODERNCALENDAR</ViewType2><ViewData><FieldRef Name=\"LinkTitle\" Type=\"CalendarMonthTitle\" /><FieldRef Name=\"LinkTitle\" Type=\"CalendarWeekTitle\" /><FieldRef Name=\"LinkTitle\" Type=\"CalendarWeekLocation\" /><FieldRef Name=\"LinkTitle\" Type=\"CalendarDayTitle\" /><FieldRef Name=\"LinkTitle\" Type=\"CalendarDayLocation\" /></ViewData><Toolbar Type=\"Standard\"/></View>

Correct HtmlSchemaXml Example:

<View Name=\"{F0105F06-6621-44AD-9E25-F3D0928D3048}\" Type=\"HTML\" DisplayName=\"Calendar\" Url=\"/sites/Test-1/Lists/Calendar/Calendar.aspx\" Level=\"1\" BaseViewID=\"1\" ContentTypeID=\"0x\" ImageUrl=\"/_layouts/15/images/generic.png?rev=47\"><Query /><ViewFields><FieldRef Name=\"Start_x0020_Date\" /><FieldRef Name=\"End_x0020_Date\" /><FieldRef Name=\"LinkTitle\" /></ViewFields><RowLimit Paged=\"FALSE\">30</RowLimit><ViewType2>MODERNCALENDAR</ViewType2><Toolbar Type=\"Standard\" /><ViewData><FieldRef Name=\"LinkTitle\" Type=\"CalendarMonthTitle\" /><FieldRef Name=\"LinkTitle\" Type=\"CalendarWeekTitle\" /><FieldRef Name=\"LinkTitle\" Type=\"CalendarWeekLocation\" /><FieldRef Name=\"LinkTitle\" Type=\"CalendarDayTitle\" /><FieldRef Name=\"LinkTitle\" Type=\"CalendarDayLocation\" /></ViewData><XslLink Default=\"TRUE\">main.xsl</XslLink><JSLink>clienttemplates.js</JSLink><ParameterBindings><ParameterBinding Name=\"NoAnnouncements\" Location=\"Resource(wss,noXinviewofY_LIST)\" /><ParameterBinding Name=\"NoAnnouncementsHowTo\" Location=\"Resource(wss,noXinviewofY_DEFAULT)\" /></ParameterBindings></View>

My question is, is there a better way to do what I'm trying to do OR is there a way to specify the Start and End dates that the calendar view will use through the REST API call?


Solution

  • Try the following -

    {
        "parameters": {
            "__metadata": {
                "type": "SP.ViewCreationInformation"
            },
            "Title": "CalendarViewTitle",
            "ViewFields": {
                "__metadata": {
                    "type": "Collection(Edm.String)"
                },
                "results": [
                    "StartDate",
                    "EndDate",
                    "Title"
                ]
            },
            "ViewTypeKind": 1,
            "ViewType2": "MODERNCALENDAR",
            "ViewData": "<FieldRef Name=\"Title\" Type=\"CalendarMonthTitle\" /><FieldRef Name=\"Title\" Type=\"CalendarWeekTitle\" /><FieldRef Name=\"Title\" Type=\"CalendarWeekLocation\" /><FieldRef Name=\"Title\" Type=\"CalendarDayTitle\" /><FieldRef Name=\"Title\" Type=\"CalendarDayLocation\" />",
            "CalendarViewStyles": "<CalendarViewStyle Title=\"Day\" Type=\"day\" Template=\"CalendarViewdayChrome\" Sequence=\"1\" Default=\"FALSE\" /><CalendarViewStyle Title=\"Week\" Type=\"week\" Template=\"CalendarViewweekChrome\" Sequence=\"2\" Default=\"FALSE\" /><CalendarViewStyle Title=\"Month\" Type=\"month\" Template=\"CalendarViewmonthChrome\" Sequence=\"3\" Default=\"TRUE\" />",
            "Query": "",
            "Paged": true,
            "PersonalView": false,
            "RowLimit": 0
        }
    }
    

    Important points to note above -

    1. RowLimit is set to zero - this is to ensure all items for the current month/week/day are fetched correctly.
    2. StartDate is mapped to 0th entry in ViewFields
    3. EndDate is mapped to 1st entry in ViewFields
    4. ViewData has 5 FieldRef entries - 1 for month view and 2 each for week and day view. The fields are used as 'Title' for respective visualizations. If this is missing, you will see the popup to 'fix' calendar view.
    5. CalendarViewStyles has 3 CalendarViewStyle entry - will be used in future. Even if this is missing, View creation will succeed.
    6. ViewType2 is MODERNCALENDAR
    7. ViewTypeKind is 1 - which maps to HTML.
    8. Query can be set if required.

    Let me know if you have any questions.