Trigger a Microsoft Flow when A document is Checked In
I made a flow for a Document Library in SharePoint Online.
The flow:
- Waits for a document in the library to be checked in
- Sends an email to the manager of the user who checked in the document
- Waits for the manager to approve or reject
- If approved, update the "Approval Status" metadata of the document to "Approved"
This sounds like something that'd be easy to do out of the box, but the trigger for "When a document is checked in" doesn't exist for Document Libraries, only for lists.
Here's how I did it: First, I did some looking around for how others have solved this and managed to find this helpful forum post.
In a blank flow, I added:
- When a file is created or modified (properties only)
- Site address from the drop down list
- Library name from the drop down list
- I left folder blank as it wasn't needed
- Get file metadata using path
- Site address from the drop down list
- File Path from dynamic content - choose "Full Path" from "When a file is created or modified (properties only)"
Next I added a check to see if the doc is already approved. If it is, terminate the flow. 3. Condition
- Content Moderation Status is equal to Approved
- if yes, terminate the flow
- No need to add anything to the "if no" side
- Send an HTTP request to SharePoint
- Site address from the drop down list
- Method GET
- URI I used is
_api/web/getFileByServerRelativeUrl('/sites/YOURSITEHERE/@{triggerOutputs()?['body/{Path}']}@{triggerOutputs()?['body/{FilenameWithExtension}']}')/checkOutType
Make sure you manually select "Folder path" and "File name with extension" from the dynamic content list, leaving them from the copy/pasted code triggered an error in the flow checker
- Leave the rest blank
- Parse JSON
- Content is dynamic content "body" from "Send an HTTP request to SharePoint"
- Schema is
{
"type": "object",
"properties": {
"d": {
"type": "object",
"properties": {
"CheckOutType": {
"type": "integer"
}
}
}
}
}
- Get user profile (V2)
- User (UPN) is dynamic content from "When a file is created or modified (properties only)" - "Modified By Email"
- Get manager (V2)
- User (UPN) is dynamic content from "Get user profile (V2)" - "User Principal Name"
- Start and wait for an approval
- Approval type is (for me) Approve/Reject - First to respond
- Title is custom for you, I used "DisplayName has requested approval for File name with Extension"
- Assigned to is dynamic content from "Get user profile (V2) - Mail
- Details is optional
- Item link is dynamic content from Get file metadata using path - "Link to item"
- Item link description is dynamic content from Get file metadata using path - "Name"
- Next is another condition.
- Dynamic content from Start and wait for an approval - "Outcome"
- is equal to Approve
- If yes - Send an email notification (V3)
- To is dynamic content from Get user profile (V2) - "Mail"
- Subject is your custom content
- Body is your custom content
- Set content approval status
- Site address is from the dropdown list
- Library name from dropdown list
- ID is dynamic content from Get file metadata using path "ItemID"
- Action is "approve"
- Comments is your custom content
- Etag is dynamic content from Get file metadata using path "Etag"
And finally If no sends a copy of the email above but informing of rejection instead of approval.
This flow took a fair amount of trial and error, but this final version works alright. I check to see what the approval status is early in the flow because of my current issue, which is that this flow triggers twice. Still working on fixing that properly, but the flow cancels itself if the doc is already approved so not a big deal I guess.