mandag 17. desember 2018

Approval of major version documents in Microsoft Flow

This fall I have been working with modernization and upgrade of a solution build in SharePoint Online. The solution used SharePoint document libraries and custom lists as data sources and SharePoint Workflow for approval of documents. On top of all this, a SharePoint App hosted in Azure (not really relevant in this context though).

So, apart from upgrading the SharePoint App to modern technology (SPFx), the job included recreating the approval workflows built in SharePoint Workflow - in Microsoft Flow.

One of the requirements for this workflow, was that it shouldn't start unless a user checked in a major version of a document. Now, I soon found that Flow doesn't have any triggers or actions that handle this specifically. Flow provides triggers for when a new file is added and when a file is added or modified (in addition to a range of other triggers of course, but those aren't relevant here).

Keep in mind that the document library we are working with, has content approval turned on, as well as minor versions and required checkin/checkout. The most important thing here is content approval though.

So, how do we solve this? Behold, the Send an HTTP request to SharePoint action.
The only way to figure out if a file is awaiting approval (through the OOTB SharePoint approval mechanism) is to query the ModerationStatus field for the file. This field is a semi-hidden field. You can display it in your views, but it will not appear in edit forms.
Now, this field can have the following values (enum-value in parentheses):

  • Approved (0)
  • Denied (1)
  • Pending (2)
  • Draft (3)
  • Scheduled approval (4)
I have used the When a file is created or modified (properties only) action

The next step is to add a query SharePoint for the ModerationStatus.

Go ahead and and a Send an HTTP request to SharePoint action and feed it with the following settings (change the values to target your tenant):

  • Method: GET
  • Uri: _api/web/lists/getbytitle('')/items()?$select=OData__ModerationStatus

Next, add a Parse JSON action in order to parse the response returned by SharePoint. Use the Body from the HTTP Request action in the Content field and the following JSON Schema:

{
"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"
}
}
}
}
}
You can put the value (OData__ModerationStatus) in a variable or you can use it directly in a condition. The important thing is to check if the value is 2 - which is Pending.

From here on you can do your magic with Approval actions and whatever else you need.

Hope this was helpful.

1 kommentar:

  1. Hello,

    I am trying to get the above to work, however I can only get it to work in a list library. When trying to apply it to a document libtary it does not work. I have attempted to modify the REST request for a doucment library, but I am not having much luck.

    I have tried with the below uri

    _api/web/GetFolderByServerRelativeUrl('$FolderPath')/Files('$FileName')?$select=OData__ModerationStatus

    However this is obviously not correct, so I am at a dead end?

    Any direction you could give would be much appreciated.

    Thanks

    SvarSlett