Recently, Microsoft Flow team has released a new SharePoint HTTP Request connector which would be extremely useful in automating the business processes.  We could use the HTTP connector to achieve this same results, but with this connector, we don’t need to worry about application registration and authentication token etc.   We can run any SharePoint REST API endpoint.

In this blog, I would try and address a very common scenario for Power Users /SharePoint Admins, i.e. to create multiple document libraries. I will be using a list provided in an Excel document.  I suppose we can also create libraries with the unique permissions, but for this blog, I will keep it simple.  Here is what the full Flow looks like:

MS Flow – Create multiple document libraries

STEPS

  • I have a custom list where admin user will provide a site URL where these libraries will be created.  It will be a manual trigger which will ask user to upload a file. Let’s assume that a template file is provided to the users which they will complete in the given format.  I am also assuming the person creating this request will have access to the site where document libraries would need to be created.

  • I had to copy the file uploaded by the user to my OneDrive as currently, it is not possible to read an Excel file from the SharePoint document library.  I have used the item id as the filename to ensure that it will always be unique.  It will be replaced if the file already exists (below screenshot is for three separate requests, all the multiple libraries details)
  • After the file is uploaded, we will get all the rows from the file which contains three columns i)Title, ii) URL, and iii) Description.

  • We will need a For each loop to read all the rows from the Excel file.  Important to note: Flow doesn’t know yet the details of the file so it won’t show you anything in the ‘Dynamic content’.  To overcome this, I selected the an existing Excel template in my OneDrive and after I got the ‘Dynamic content’ I changed the file name to ID so it gets dynamically created.  You can obviously write the expression.

  • Now the real bit, we will need to make a POST request to SharePoint REST API endpoint/uri.  To create list the URI is “_api/web/lists”.  The body of the request must include:
{
 "__metadata": {
 "type": "SP.List"},
 "BaseTemplate": 101, // 101 is template for document library, full list in the reference below 
 "ContentTypesEnabled": false, // if you want to enable content type, change this to true
 "Description": "Optional Document Library Description",
 "Title": "SharePointer" // This will be part of the url for the library
}

  • We then Parse the response data through Parse JSON connector, in order to get the guid/list id created.  I have initially created the library with the short title without spaces to avoid getting %20 in the URL. After library is created, I have updated the libraries with the longer title (with spaces) this way we will get the nice short URLs.
  • Constructing the URI for this update was slightly challenging as this endpoint must include a single quote with the list guid/id, for example:
/_api/web/lists('b0e48240-4e6f-4ad9-9f47-f4eafc5d1f69')

To achieve this I used concatenate expression to include the list id/guid which is in the response received after a library has been successfully created.

concat('/_api/web/lists(''',body('Parse_JSON')?['d']?['Id'],''')')

Note the extra single quotes at the start of parentheses and before the  second last parentheses

In the update POST request we will need to include the following in the Header:

{ "Accept": "application/json;odata=verbose", 
 "Content-Type": "application/json;odata=verbose", 
 "X-HTTP-Method": "MERGE", 
 "IF-MATCH": "*" 
}
And pass the body with the new longer Title which is in the Excel file ‘Display Name’
{
 "__metadata": {"type": "SP.List"},
 "Title": "New Document Library Title"
}

Final results – multiple document libraries created within few seconds and no ugly URLs.  More more clicking UI to create libraries..!.

Reference:

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *