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.
To provide a seamless user experience, we decided to do the following:
- Create a document library template (.stp) with all the prerequisite folders and content types applied.
- 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.
- Whenever the title, owners or contributors are changed, the destination document library will be updated.
The solution has two main elements to automate this process
- Microsoft Flow – Trigger when an item is created or modified
- Two Azure Functions – Create the library and update permissions
The broad steps and code are as follows
- 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.
- The flow will call an Azure function via an HTTP Post action in a Function. Below is the configuration of this.
- For the “Create Library” Azure function, create a HTTP C# Function.
- In the Azure Function, open Properties -> App Service Editor. Then add a folder called bin and then copy two files to it.
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.
- After copying the files, reference them in the Azure function using the below code
- Then create the SharePoint client context and create a connection to the source list.
- After that, use the ListCreationInformation class to create the Document library from the library template using the code below.
- After the library is created, break the role inheritance for the library as per the requirement
- Update the library permissions using the role assignment object
- 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()
- 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