Publish files in SharePoint using Microsoft Flow and Column formatting
When using versioning with major/minor versions in SharePoint, you will come to a point that you need to publish your file. Out-of-the-box SharePoint provides you with that functionality by selecting More > Publish in the context menu of the file.
But what if you want to do more than just publishing the file (e.g. Approval before publishing)? You use Microsoft Flow of course!
Configuring the Flow
I want the Flow to be triggered manually, so I create a Flow with the trigger ‘For a selected item’. With that option, I can create a custom Flow button using column formatting (more on that later) and I can ask for comments (the same as you would be asked if you use the out-of-the-box Publish option in SharePoint). I also want the file to be copied to another library after the file has been published, so lets go and jump into it.
Steps involved
To make sure all requirements are met (publish file and then copy the file), we need to make sure all of the following steps are followed:
- Ask for comments
- Configure variables
- Check out the file
- Publish the file
- If the file cannot be published (e.g. when required metadata isn’t filled), undo checkout and stop the Flow
- Copy the file
Sounds pretty straight forward, doesn’t it? Well, unfortunately Microsoft Flow doesn’t offer us out-of-the-box actions for checking out and publishing files YET. It is announced that these actions will come, so until then we must use HTTP requests to perform these actions.
Ask for comments
Since we are using the ‘For a selected item’ trigger, we can add Input parameters that the user needs to fill before starting the Flow by selecting the ‘Add an input‘ option in your trigger. We are asking for comments, so we can select the Text option and give it a name and a description:
Configure variables
Since we are using a few actions that use the same strings (such as site URL, source library and destination library) it is easier to put these strings in a variable. If anything changes of if you want to use this Flow somewhere else you only have to change the variables and not each action by itself. Please note that you do always need to specify the Site address and Library name in your For a selected item trigger. You cannot use variables in your trigger!
The following variables need to be initialized:
- siteAddress
This is the full path of your SharePoint site (e.g. https://contoso.sharepoint.com/sites/publish) - relativePath
This is the relative path of your SharePoint site (e.g. /sites/publish) - sourceLibrary
This is the name of your source library (e.g. Source library) - destinationLibrary
This is the name of your destination library (e.g. Destination library)
Check out file
The endpoint for checking out files is:
/_api/web/GetFileByServerRelativeUrl('<ServerRelativeUrl>')/CheckOut()
You can call this endpoint by using the Send an HTTP request to SharePoint action in Microsoft Flow. Make sure you set the Site Address to our siteAddress variable and select the POST method. The endpoint stated above needs to be configured in the Uri field:
To get the ServerRelativeUrl for you file, you must combine your relative site URL (which is the relativePath variable), followed by a forward slash and the ‘Folder path‘ and ‘File name with extension‘ properties. Since we are using variables for our ‘Get file properties’ action, we cannot select them as Dynamic Content so we must configure them manually by using the body() expression.
The internal names for these fields which are Path and FilenameWithExtension. This results in the following expressions:
-
body('Get_file_properties')?['{Path}']
-
body('Get_file_properties')?['{FilenameWithExtension}']
Publish file
To publish the file, we need to do the same as the Check out action, only with a slightly different endpoint, which is:
/_api/web/GetFileByServerRelativeUrl('<ServerRelativeUrl>')/CheckIn(comment='<comment>',checkintype=1)
To make sure the comments which are filled by the user are being used while publishing the file, we need to put out Comments input from the Dynamic Content here. checkintype=1 means that you will check in a major version.
Undo checkout
When the publish action cannot be completed (e.g. when required metadata isn’t filled), we need to make sure the check out action is undone. To achieve this, we must use the ‘Send an HTTP request to SharePoint’ action, but only when the publish file action has failed or timed out.
When you added the ‘Send an HTTP request to SharePoint’ action, you must select the ellipsis and select ‘Configure run after’. This window allows you to configure when the action should be ran:
- When the previous action is succesful
- When the previous action has failed
- When the previous action is skipped
- When the previous action has timed out
In this case, we need to select ‘has failed’ and ‘has timed out’
Now we can configure our action. To undo the check out, we need to do the same as the Check out action, only with a slightly different endpoint, which is:
/_api/web/GetFileByServerRelativeUrl('<ServerRelativeUrl>')/UndoCheckOut()
After this action, you can add a Terminate action to make sure your Flow will be stopped.
Copy file
When the file is successfully published, you can use the ‘Copy file’ action to copy the file to your destination library. Because we already added the undo check out option beneath our publish file action, we need to add a parallel branch to our undo check out option in which we add the ‘Copy file’ action.
Within this action, you need to configure the following properties:
- Current site address
This is the site where the document originates from, which in our case is the siteAddress variable - File to copy
This is the unique identifier of the document. The internal name of this property is Identifier, so the body() expression for this property is:body('Get_file_properties')?['{Identifier}']
- Destination site address
This is the site where the document needs to be copied to, which in our case the the same as the Current site address which is the siteAddress variable - Destination folder
This is the folder where the document needs to be copied to. Please note that this is both library name and (if applicable) subfolder(s). We use the Path property for this in which we replace the source library name to the destination library name, using the replace() expression. But this property expects its value without a trailing slash and the Path property always gives a trailing slash, so we need to remove that from our replace() result:substring( replace( body('Get_file_properties')?['{Path}'], variables('sourceLibrary'), variables('destinationLibrary') ), 0, sub( length( replace( body('Get_file_properties')?['{Path}'], variables('sourceLibrary'), variables('destinationLibrary') ) ), 1 ) )
Breaking this down into pieces:
- The replace() expression replaces the value in our sourceLibrary variable from the Path of the file with the value in our destinationLibrary variable
- the sub() expression subtracts 1 character from the length of our replace() expression. So if the length of the replaced Path was 11, it now will be 10
- the substring() expression uses X characters from the outcome of our replace() expression, starting at 0, where X is the outcome of our sub() expression
I put this entire expression into a separate Compose action, to make it more manageable. Also make sure you put a forward slash in front of your expression!
- If another file if already there
With this property, you configure what needs to be done if a document already exists: Copy with a new name, Fail this action or Replace. In our case, we use the Replace option.
Our entire flow looks like this:
You can customize this as you wish by adding approvals or other actions to it.
Creating the Flow button
Now that we’ve created the Flow, we need to provide the user with easy access to publish a file. We can do that by using column formatting to create a flow button. More information on this can be found here.
The joy of modern SharePoint is that you can create a nameless field, which can be very handy if you want to use Flow buttons. Just select + Add column in your library and put a space into the Name field and voila, there’s your nameless column. To enter your column formatting into your field, select the down arrow of the field, click Column settings and select Format this column. The Format column pane will appear in which you need to paste the column formatting JSON
I want the Publish button only to be visible on documents and not on folders or document sets, so I played around a bit. I also tried to do something with the Version field (only show the button on minor versions), but since this is a system field, it cannot be used in your column formatting unfortunately. This resulted in the following JSON:
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json", "elmType": "span", "style": { "color": "#0078d7" }, "children": [ { "elmType": "span", "attributes": { "iconName": "=if([$File_x0020_Type] != '','PublishContent', '')" } }, { "elmType": "button", "style": { "border": "none", "background-color": "transparent", "color": "#0078d7", "cursor": "pointer" }, "txtContent": "=if([$File_x0020_Type] != '','Publish', '')", "customRowAction": { "action": "executeFlow", "actionParams": "{\"id\": \"123456ab-7cde-8f9g-h0i1-234j5kl67890\"}" } } ] }
Please make sure you use the correct Flow ID in your actionParams!
Saving the column formatting, will give you a nice Publish button for your documents only. When clicking on it, the Flow pane will appear asking you to enter the comments:
When the Flow ran successfully, the document is published with the given comments:
When I add a required field, don’t fill it and start the Flow again, the publish file action will fail and the undo check out action will be triggered:
4 Replies to “Publish files in SharePoint using Microsoft Flow and Column formatting”
Hi Rik,
nice approach – thanks for sharing.
How can we avoid that the users accidently use the standard “publish”-menu item and not the Flow-workflow?
I only know the SPFx approach for hiding the menu item but thats quite a huge effort….
br,
Stefan
As far as I know, there is no easy way to hide the (context) menu or items within the menu. As you said, SPFx can do it for you but that indeed needs some effort.
To enable the same functionality on the standard publish button. You could create a similar flow that is auto-started when the file properties are modified and set a condition on the trigger of the flow to only auto-start when the content status is ‘Pending’.
True, but beware that this may cause a significant load on your flow runs. Better would be to use a trigger condition, so that the flow would only trigger when a specific condition is met