Create a grouped by collapsed view

Within SharePoint Online it is possible to create views for lists and document libraries. In this article I will show you how to create a view which is grouped by a specific field and collapsed by default via a Power Automate flow.

Inspiration

This question from Insaneous:

I have to create over 500 libraries. To speed things up, I wanted to use Power Automate to create all the libraries from a list of names from a Spreadsheet. Works great.

I then needed to create a series of views for each library (they are all the same). I need a WiP view that filters by a specific column and a Records view which does the same. I’ve managed to figure that out too. One thing I am struggling with however is grouping by a column.

I have a column called CAT which I want both views to group by. But for the life of me I cannot figure it out.

Power Users Community thread: Creating custom views using HTTP Rest for SharePoint – Grouping.

createaview_groupedby_collapse_result

Create a View

There are a couple of methods to create a view.

1. The Views method and a SP.View object.
2. The SP.ViewCollection.Add method and a SP.ViewCreationInformation object.

The documentation demonstrates that these methods will have either a SP.View.ViewQuery property or a SP.ViewCreationInformation.Query property which you can set.

CAML

The language used in these query properties is the Collaborative Application Markup Language aka as CAML.

CAML is an XML-based language that is used in Microsoft SharePoint Foundation to define the fields and views that are used in sites and lists.

For me that was a trip down memory lane, because I have been using that a lot in my SharePoint on-prem days.

But if you are new to this I would suggest to have a look at the Group By element documentation.

That element also has Collapse boolean attribute which we can use in our CAML Query when we are creating the new view.

Flow Setup

For this sample I have chosen to use the SP.ViewCollection.Add method. I prefer it because you can also set the SP.ViewCreationInformation.viewFields property in the same request. Which does not seem possible with the SP.View.viewFields property.

createaview_groupedby_collapse

1. Add a Manually trigger a flow action.

manuallytriggeraflow

2. Add a Initialize variable action.

listname_workitemtracker

a. Provide a Name, in this example ListName
b. Select a Type, String
c. Provide a Value, in this Work Item Tracker

3. Add a Send an HTTP request to SharePoint action.

createaview_groupedby_collapse_http

a. Select your preferred site in the Site Address
b. Use the POST method
c. Use the URI from the code snippet below

d. Use the Headers from the code snippet below

e. Use the Body from the code snippet below

Happy testing!

You may also like...

1 Response

  1. Michael Martin says:

    Hi,

    Thanks for the great article!

    Is it possible to create a view with 2 levels? e.g. if there are 2 choice columns “Customer’ and “Location”, group by customer, then location as the 2nd level?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.