Updating list views JSON formatters for a SharePoint list using Power Automate

Imagine a case where you want to apply the same JSON custom formatting in all SharePoint list views for a list and this list has a lot of views. You would need to do it one by one in all views, or you could use PowerShell to script it. But not all the times you are able install PowerShell in your work computer.

That’s when Power Automate comes handy, we can create a button Flow to run this action for us.

The same idea can even be reused to be part of a bigger automation process.

Check out how to do it.

Manual Trigger

Create a flow with a manual trigger and the following parameters:

  • Site URL
  • List Name
  • Custom Formatter

And initialize variables to associate all the 3 values as below (for clarity and later manipulation).

Escaping Single Quotes from the JSON content

With a compose action as below, use the following expression to escape single quotes from the CustomFormatter, otherwise you may run into issues if the content has a single quote:

replace(variables('CustomFormatter'),'''','\''')

Retrieve the right list views

Use the Send an HTTP Request to SharePoint to retrieve all list views as below (omitting Hidden & Personal views):

Then parse the outputs from the ‘Send an HTTP request’ action using the ‘Parse JSON’ action.

You can use the following schema:

{
    "type": "object",
    "properties": {
        "value": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "CustomFormatter": {},
                    "Id": {
                        "type": "string"
                    }
                }
            }
        }
    }
}

For simplicity we are only parsing the ID and the CustomFormatter properties from the Views.

Based on the outputs of the Parse JSON action, run a patch HTTP request on each view returned, passing the ID in the endpoint:

As body for the patch statement, use the following content (outputs of the compose action as value for the CustomFormatter property):

{
'CustomFormatter': '@{outputs('Compose_-_Escape_Single_Quotes')}'
}

Then to run the flow, simply run it and pass the desired site URL, list name and JSON content:

When you run the Flow passing your custom JSON template, it will update all public views in the list with the new JSON template.

One comment

Leave a Reply

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