As of the time of writing this, Microsoft Flow does not include any SharePoint connector actions to create a document set in a document library, which is quite inconvenient.
Instead, we are left to deal with this using SharePoint’s REST API, which is not straightforward and proves to have a few gotchas to watch out for.
This is the overview of a Flow that will create a new document set in the Documents library, on trigger from a list item, just for the sake of a demo.
We will go through each action step by step below.
Before we are able to access the SharePoint REST API through Microsoft Flow, we need to setup a new app registration in the SharePoint site collection to allow Flow to authenticate using the add-in-only policy, to get an access token.
- In your site collection, as a site collection administrator, access the app registration page using the URL <site_collection_url>/_layouts/15/appregnew.aspx.
- Enter/generate the Client Id and Secret, and set the Title, App Domain, and Redirect URI.
- Click Create.
- Access the App Grant Permission page using the URL <site_collection_url>/_layouts/15/appinv.aspx.
- Use Client Id above to lookup the App.
- Set Permission Request XML using the below XML
<AppPermissionRequests AllowAppOnlyPolicy="true"> <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="Manage" /> </AppPermissionRequests>
- Click Create, then Trust It to grant the App the required permissions (Note: I’ve used Manage, but Write level should be sufficient for just adding document sets).
I’ve just used the SharePoint ‘For a selected item’ trigger so that the flow can be run from a list item.
Step 1: Parse JSON – Settings
Using the ‘Data Operations – Parse JSON’ action to define some variables that we set based on our tenant. Plug in the Client Id and Secret that you previously registered in here for authentication. The resourceId will always be “00000003-0000-0ff1-ce00-000000000000”.
Step 2: HTTP – Get AccessToken
Use the ‘HTTP – HTTP’ action to get an access token that we will need in order to access the SharePoint REST API. The variables are taken from the Settings JSON.
Step 3: Parse JSON – AccessToken Response
Parse the response from the HTTP – Get AccessToken action above in JSON. You can run the flow until Step 2 above and copy the response, and use that payload to automatically generate the schema for you.
Step 4: HTTP – Get List Document Set Content Type
We can now query the SharePoint REST API using the access token obtained from the action above, and setting in the Authorization header as “Bearer “.
Our first REST API call is to the target document library, querying for the Document Set content type. The filter in unencoded form used here is $filter=Name eq ‘Document Set’. You can use this to choose any content type inheriting from document set as well.
We need to do this because we need to get the content type ID of the list’s Document Set content type (which is 0x0120D520<guid>), not the web’s Document Set content type (which is just 0x0120D520), as not using the list content type ID will cause the document set to not be created correctly in SharePoint Online.
Step 5: Parse JSON – Document Set Content Type
Again, we simply parse the result from the REST API call above into JSON.
Step 6: HTTP – Create Document Set
Now we are able to use the SharePoint REST API to create our document set. Do a HTTP Post to <site_collection_url>/_vti_bin/listdata.svc/<list_name>.
Set the Slug header to <target_list_url>/<doc_set_name>|<list_doc_set_content_type_id>. This is the crucial bit to create a document set instead of a folder. In our flow, we’re setting the <list_doc_set_content_type_id> using an expression to get it from the Document Set Content Type JSON we parsed above:
Running the flow should now create a document set for us in our document library. Again, it is important to note that we cannot use the web document set content type ID (0x0120D520). If you do, it will still create the document set, but the Modern List view will not properly recognise it as a document set, and will display it as a folder instead (see Test3 vs Test4 below).
Hopefully this will be of help until Microsoft comes out with an action to simplify this.