I’ve been working on a client requirement to automate SharePoint library management via scripts to implement a document lifecycle with many document libraries that have custom content types and requires regular housekeeping for ownership and permissions.
Solution overview
To provide a seamless user experience, we decided to do the following:

1. Create a document library template (.stp) with all the prerequisite folders and content types applied.

2. Create a list to store the data about entries for said libraries. Add the owner and contributors for the library as columns in that list.

3. Whenever the title, owners or contributors are changed, the destination document library will be updated.

Technical Implementation
The solution has two main elements to automate this process

1. Microsoft Flow – Trigger when an item is created or modified

2. Two Azure Functions – Create the library and update permissions

The broad steps and code are as follows:
1. When the flow is triggered, we would check the status field to find if it is a new entry or a change.
Note: Since Microsoft flow doesn’t have conditional triggers to differentiate between create and modified list item events, use a text column in the SharePoint list which is set to start, in progress and completed values to identify create and update events.
2. The flow will call an Azure function via an HTTP Post action in a Function. Below is the configuration of this.
AzureFunctionFromFlow
3. For the “Create Library” Azure function, create a HTTP C# Function in your Azure subscription.
4. In the Azure Function, open Properties -> App Service Editor. Then add a folder called bin and then copy two files to it.

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Client.Runtime

KuduTools_AzureFunction
Create Lib App Service Editor
Please make sure to get the latest copy of the Nuget package for SharepointPnPOnlineCSOM. To do that, you can set up a VS solution and copy the files from there, or download the Nuget package directly and extract the files from it.
5. After copying the files, reference them in the Azure function using the below code

#r "Microsoft.SharePoint.Client.dll"
#r "Microsoft.SharePoint.Client.Runtime.dll"
#r "System.Configuration"
#r "System.Web"

6. Then create the SharePoint client context and create a connection to the source list.
7. After that, use the ListCreationInformation class to create the Document library from the library template using the code below.

8. After the library is created, break the role inheritance for the library as per the requirement
9. Update the library permissions using the role assignment object
10. To differentiate between People, SharePoint Groups and AD Groups, find the unique ID and add the group as per the script below.

Note: In case you have people objects that are not in AD anymore because they have left the organisation, please refer to this blog for validating them before updating – Resolving “User not found” issue while assigning permissions using SharePoint CSOM

      Note: Try to avoid item.Update() from the Azure Function as that will trigger a second flow run, causing an iterative loop, instead use item.SystemUpdate()
11. After the update is done, return to the Flow with the success value from the Azure Function which will complete the loop.
As shown above, we saw how we can automate document library creation from a template and permissions management using Flow and Azure Functions

Category:
Application Development and Integration, Azure Platform, Office 365, SharePoint
Tags:
, , ,

Join the conversation! 1 Comment

  1. Hi Asish,

    I am working on the same requirement whereby i have saved a library as template which contains document sets and i would like to automate the creation of document library from the template. I am getting stuck at step2 in this article. can you please assist?

    Regards
    Amit

Comments are closed.