Microsoft SharePoint allows us to use remote provisioning for our SharePoint Online site collections and SharePoint Add-ins. You can easily create a provisioning template and then apply and reuse the same by using simple PowerShell scripts or C# code. It is explained in detail at the Microsoft site.
To move all files into a SharePoint library, take the following snippet.
- <?xml version="1.0"?>
- <pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2018/01/ProvisioningSchema">
- <pnp:Preferences Generator="OfficeDevPnP.Core, Version=2.11.1701.1, Culture=neutral, PublicKeyToken=3751622786b357c2" />
- <pnp:Templates ID="CONTAINER-TEMPLATE-4570F0FB6B05425ABA32D6C13A7BD22C">
- <pnp:ProvisioningTemplate ID="TEMPLATE-4570F0FB6B05425ABA32D6C13A7BD22C" Version="1" BaseSiteTemplate="STS#0">
- <pnp:Files>
- <pnp:Directory Src="Pics\" Folder="CustomLibrary" Overwrite="true" Recursive="true" Level="Published"></pnp:Directory>
- </pnp:Files>
- </pnp:ProvisioningTemplate>
- </pnp:Templates>
- </pnp:Provisioning>
I am picking the PNP schema released in Jan 2018. I just mentioned the Source folder from which I am running the below scripts.
PowerShell ISE
Before starting the PowerShell ISE, make sure that you have installed SharePoint Online Management Shell. If not, click
here to download & install it.
Folder Structure in Windows
In the schema, I mentioned the Source folder as “Pics” in Src of Pnp:Directory node.
- <pnp:DirectorySrc="Pics\"Folder="CustomLibrary"Overwrite="true"Recursive="true"Level="Published"></pnp:Directory>
Also, I created the folder in the location with the same name from where I am going to run my script. I added a few files inside my directory.
Before provisioning the XML, we should make sure that we are connected to a SharePoint online site and a library is created with the name “CustomLibrary”.
To connect to SharePoint Online use,
Connect-PnPOnline-Url"https://########.sharepoint.com/sites/PSSamples"
It will ask for user credentials to get connected, give the Office365 user credentials.
Then, create your library in the SharePoint Site. I created a Document Library and named it as "CustomLibrary".
Once the PowerShell command is connected to SharePoint Online, use the below cmd to apply the provisioning.
Apply-PnPProvisioningTemplate-Path".\MoveDirectory.xml"
After running the command, you could see that the files been moved to your library.