“I’d like to duplicate structure of an existing SP document library, how can I create its copy on another site, e.g. with Power Automate?”
If you use SharePoint as a data storage, there’re two data types you can use – items and documents. Items are stored in lists, documents in document libraries. But often you need more columns for all the data than the default list/library gives you, so you keep adding more. And then you realise you need the same on another SP site. You already spent some time adding all the columns, it would be a shame to keep it only on a single SP site. Why not copy it to multiple sites, to reuse it?
I already explained how to copy existing lists, this article is about libraries. To make it a bit more interesting, it’ll use also a different approach.
Create a new document library
It’s a more old school approach, but at the same time maybe a more reliable one. This one doesn’t rely on complex functions, it creates the library step by step – library first, columns later. It can be also used to copy lists with only a cosmetic change.
Firstly, create the new library using an HTTP request to SharePoint to the target site.
Method: POST
Uri: _api/web/lists/
Headers:
{
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
}
Body:
{ "__metadata": {
"type": "SP.List"
},
"Title": "<LibraryName>",
"Description": "",
"BaseTemplate": 101
}
Note: this is the step where you differentiate whether it should be a list (BaseTemplate 100) or a document library (BaseTemplate 101).
It’ll create a new blank library with only the basic columns.
List columns in both libraries
As the columns in the original library can change, you don’t want to hard code them in the flow. You want to take the columns that exist right now, when the library is copied. Let’s do it by comparing the columns in those libraries and identifying the missing ones.
List all the columns in the original library with another HTTP request.
Method: GET
Uri: _api/web/lists/getByTitle('<originalLibraryName>')/fields
Do the same for the new library. Here I’m referencing the new list title with an expression, using the output from the creation HTTP request.
body('Send_an_HTTP_request_to_SharePoint_-_create_new_library')?['d']?['title']
Compare the columns and find the differences
Now you have columns from both the libraries for comparison. But before you do that, there’s a need for a small “cleanup”. The reason is that the libraries contain a lot of hidden columns, something you’re not interested in. You want only the columns you created, not the ones that are added automatically by SharePoint.
Filter from the original library columns only the ones whose ‘Group’ is equal to ‘Custom Columns’.
body('Send_an_HTTP_request_to_SharePoint_-_get_original_lib_columns')?['d']?['results']
item()?['Group']
Select only the ‘Title’ from the remaining columns in both the libraries, and find the differences as explained in the previous article.
The output of this ‘Filter array’ will be only the columns that exist in the original library but not in the new one – the columns to add.
Get the columns XML schema and create them in the new library
Once you have the column names, all you that’s left is to get their XML schema and recreate them in the new library.
Add an ‘Apply to each’ and for each column get its XML schema.
Extract the schema from the HTTP request, and use it to create the column in the new library. The library name is again extracted directly from the creation HTTP request as shown above.
body('Send_an_HTTP_request_to_SharePoint_-_get_OG_column_schema')?['d']?['SchemaXml']
And that’s it, you just copied a structure of an existing document library. Now you can add the columns to the views, or even copy content from the original library.
Summary
As you can see, it’s not that complicated to copy a document library with a Power Automate flow. This approach is a bit different from the one copying SP lists, but it’s also a bit less of a ‘black box’. Here you know exactly what’s going on in every step. Firstly, create the new, blank library. Secondly, identify the columns to add. Thirdly, get their XML schema and use it to recreate the columns. No complex functions that can change in time, just a bunch of good, old REST API calls.
And with a cosmetic change it can be used also to create lists, just use BaseTemplate 100 instead of 101.
1 thought on “How to copy a SP document library with Power Automate”