Creating SharePoint Online Document Set in Microsoft Flow

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.

MS Flow Create Doc Set - Overview.png

We will go through each action step by step below.

App Registration

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.

    1. In your site collection, as a site collection administrator, access the app registration page using the URL <site_collection_url>/_layouts/15/appregnew.aspx.
    2. Enter/generate the Client Id and Secret, and set the Title, App Domain, and Redirect URI.
      MS Flow Create Doc Set - AppRegNew.png
    3. Click Create.
    4. Access the App Grant Permission page using the URL <site_collection_url>/_layouts/15/appinv.aspx.
    5. Use Client Id above to lookup the App.
    6. Set Permission Request XML using the below XML
      <AppPermissionRequests AllowAppOnlyPolicy="true">
        <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="Manage" />
      </AppPermissionRequests>
      
    7. 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).

Flow Trigger

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

MS Flow Create Doc Set - 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

MS Flow Create Doc Set - Get AccessToken.png

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

MS Flow Create Doc Set - Parse JSON AccessToken Response.png

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

MS Flow Create Doc Set - Get List Document Set Content Type.png

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

MS Flow Create Doc Set - Parse JSON Doc Set Content Type.png

Again, we simply parse the result from the REST API call above into JSON.

Step 6: HTTP – Create Document Set

MS Flow Create Doc Set - Create Doc Set.png

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:

first(body(‘Parse_JSON__-_Document_Set_Content_Type’)?[‘d’][‘results’])?[‘StringId’]

Summary

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).

MS Flow Create Doc Set - Created Doc Sets.png

Hopefully this will be of help until Microsoft comes out with an action to simplify this.

9 thoughts on “Creating SharePoint Online Document Set in Microsoft Flow

    • Thanks Haniel. Since this article was published MS Flow has introduced the ‘Send an HTTP request to SharePoint’ action that makes it much simpler to access the SharePoint REST API. It handles the authentication for us, so we no longer have to manually do an app registration or get access tokens. You can skip straight to to step 4 and use the new action instead of the generic HTTP actions, plugging in the same Uri and Headers, minus the Authorization header.

      But otherwise, the app registration should work for all users as long as you’re sending it the correct client id and secret when trying to get an access token.

      Like

      • Hi Lee Yi Chiun,

        Thanks a lot for sharing this flow description.
        I heard that MS is working to fix this issue for Q1 2019.
        However, I would like to apply your workflow from now. I am a beginner, first one I am doing and I am not very familiar with the built in steps.
        I started form step 4 using same layout as you and I get an error message explaining that the uri is not valid. Seems related to the filter…
        For step 5, how to write the code in the schema field, should it be automatically populated ?
        Could you help to troubleshoot ?

        Many thanks in advance,

        Best regards

        Jeremy

        Like

      • Hi Jeremy,

        Not knowing the URI you’re using, I’m afraid I can’t help troubleshoot. I’d recommend that you debug it by querying the SharePoint REST API from a web browser. You should be able to tweak it and get the response back instantly.

        With step 5, I’ve just used the JSON payload returned from step 4 to generate the schema in the Flow action itself.

        Hope that helps.

        Like

  1. Pingback: Microsoft Flow – Create a Document Set « Karine Bosch’s Blog
    • Hey Nick, pretty sure the standard ‘SharePoint – Create file’ Flow action should work, if you set the folder path to your document set, since the document set should behave as a folder does.

      Like

  2. Have a question if anyone has come across this. The method seems great. I am using the Sharepoint “Send an HTTP request” which is now in flow.
    My list name is “PurchaseOrders” although the “display” name is “Purchase Orders”. The library is on the main site.
    I am using the following params:

    Site address: https://mydomain.sharepoint.com/
    Method: POST
    Uri: _vti_bin/listdata.svc/PurchaseOrders
    Accept: application/json;odata=verbose
    Slug: https://mydomain.sharepoint.com/Purchase%20Orders/test123|0x0120D52000117E905E315222488885D1E953316FFA00AEAE21A1BDC7084xxxxxxxxxxxxxxxxxx

    I get an error 400 which says “Bad Request – Invalid Hostname”

    I have tried every combination and can’t get anything to work.
    just calling https://mydomain.sharepoint.com/vti_bin/listdata.svc/PurchaseOrders from my browser does show the correct contents of the library.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s