Recently we have finally been able to add a modern calendar view to a list in SharePoint Online but only through the UI. Before this a calendar view was only available in SharePoint classic mode. I wanted to automate this ne modern calendar view creation and in order to do this I turned to my trusty friend PnP PowerShell. I figured out that it needed to be done using SharePoint REST API by creating a new SharePoint list view using ViewType2: “MODERNCALENDAR”.

Old Classic Calendar view in the background vs New Calendar view in the foreground

Create New Modern Events SharePoint List with Modern Calendar View

I will show you below how you can create a new Events list using PnP PowerShell and set the default view to be a modern calendar view.. You can also adapt and modify steps 2, 4, 5 & 6 to add a new modern calendar view to an existing SharePoint list.

The steps to create a new modern events list using PnP PowerShell are below:

  1. Create new SharePoint list using the events template:
  1. Set new list experience to force the list to display in the Modern experience
  1. Remove the default classic calendar view provisioned by default with the events template.
  1. Below Is the JSON body required for the add View REST API call

Note the following regarding the REST API body listed above (Credit Shreyansh Agrawal – power automate – Create Modern Calendar view for SharePoint Online List using the REST API – Stack Overflow)

  • RowLimit is set to zero – this is to ensure all items for the current month/week/day are fetched correctly.
  • StartDate (internal field name) is mapped to 0th entry in ViewFields
  • EndDate (internal field name) is mapped to 1st entry in ViewFields
  • 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.
  • CalendarViewStyles has 3 CalendarViewStyle entry – will be used in future. Even if this is missing, View creation will succeed.
  • ViewType2 is MODERNCALENDAR
  • ViewTypeKind is 1 – which maps to HTML.
  • Query can be set if required.
  1. Use the PnP command Invoke-PnPRestMethod to hit the /Lists/Views/Add end point using the method Post and then use the JSON body we just crafted above.
  1. Finally set the newly created “Calendar” view to be default for list for normal and mobile modes.

The modern calendar view is now created:

Full code in a Github Gist below:

Summary

Here I showed you using PnP.PowerShell to create a new Modern SharePoint events list with all modern views. I took me a while to work out the REST API call to create a MODERNCALENDAR view and thanks to the web developer tools in Microsoft Edge I was able to see what REST API calls were being made to create my own Modern Calendar. Hope this is helpful!

Leave a Reply