Friday, May 4, 2018

Use Microsoft Flow to implement approval of site pages


Photo by Zachary Nelson at Unsplash

Again I’m late to the party, but Microsoft has released an action to Flow called “Set content approval status”. This action enables you to add content approval not only to list elements and documents in SharePoint, but also to news pages.

Microsoft has talked about content approval for ECM scenarios with communication pages for a while, but until a proper UI is in place, you can get started today!

This is how.

First off navigate to the Site Pages library where you create your pages, and go to the versioning settings for the library.

Ensure you have turned on and least content approval and enabling major/minor versioning is also a good idea so that editors can save their work multiple times before hitting Publish for approval.

image

Next create a Flow which trigger on the Site Pages library. I like to start from the Flow UI, but you can start off any way you like.

Important! The user used for the connection in the SharePoint actions, has to be an owner on the site, in order to have approval rights for items – or given the approval right explicitly.

I will start with the SharePoint action “When an item is created or modified”. The reason for triggering on modified as well, is to cater for the major/minor versioning scenario – in order not to skip draft items.

If you’re advanced and adventurous you can check Serge Luca’s post which dives into how you can change the trigger to only trigger on items based on properties of the item.

As the Site Pages library will not list automatically for this action (nor will it for the file is created action), you have to manually enter the name or the guid of your list.

image

Choose “Enter custom value” and type the name of your Site Pages library.

image

The next step is to check for the Pending approval status, which is what we will act on. Any other approval state and we can exit the Flow.

Since I decided to complicate things with draft status, you currently have to use the new “Send an HTTP request to SharePoint” action in order to retrieve the approval status of the item, which can be done with the following REST query where the value is stored in the column _ModerationStatus, which is accessible via OData__ModerationStatus using REST.

_api/web/lists/getbytitle('Site%20Pages')/items(<id>)?$select=OData__ModerationStatus

image

In order to make it easier to check the value from the returned JSON I add a Parse JSON step with the following schema (generated from a test query)

{
    "type": "object",
    "properties": {
        "d": {
            "type": "object",
            "properties": {
                "__metadata": {
                    "type": "object",
                    "properties": {
                        "id": {
                            "type": "string"
                        },
                        "uri": {
                            "type": "string"
                        },
                        "etag": {
                            "type": "string"
                        },
                        "type": {
                            "type": "string"
                        }
                    }
                },
                "OData__ModerationStatus": {
                    "type": "number"
                }
            }
        }
    }
}

The Pending approval state has a value of 2, so for any other value we will terminate the Flow. This is easily accomplished by checking the value of the OData__ModerationStatus field.

To simplify my Flow I don’t add further commands in the Yes branch, but add them below for better readability.

image

The check is done, we know the page is in pending state, requiring someone to approve or reject it. And an easy approach to use is the Flow approval action.

image

Once the e-mail approval has taken place it’s time to use the “Set content approval status” action depending on if the approver approved or rejected the item.

When approving a page you need a value for the ETag field. To get a working ETag value use the “Get file metadata” action with the file identifier from the trigger action. One positive side of the approval action is that the Site Pages library automatically appear in the dropdown :-)

image

Looking back at the site pages library, you see the page is approved and will be visible by everyone.

image

Summary

Content approval is often an important feature of ECM, where not anyone can publish articles or news at random. Tapping into the old approval functionality in SharePoint and combining it with Microsoft Flow is a nice way to unblock this scenario. Once a user hits “Publish” on an article, we kick off an approval workflow, where the approver can approve the item directly from the e-mail message if they want to.

If you need scheduling as well, simply add a column to the Site Pages library put a date in it, and add a Delay action in your Flow to pause the actual setting of the Approval state.

The Flow today might seem a bit convoluted, but I expect a better UI integration in the future based on previous communications from Microsoft om the ECM story for Communication sites. The beauty is that this approval Flow is very generic and can be used on any site and for any library.