Move Files From Local Directory To SharePoint Library Using PNP Provisioning

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.

  1. <?xml version="1.0"?>  
  2. <pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2018/01/ProvisioningSchema">  
  3.     <pnp:Preferences Generator="OfficeDevPnP.Core, Version=2.11.1701.1, Culture=neutral, PublicKeyToken=3751622786b357c2" />  
  4.     <pnp:Templates ID="CONTAINER-TEMPLATE-4570F0FB6B05425ABA32D6C13A7BD22C">  
  5.         <pnp:ProvisioningTemplate ID="TEMPLATE-4570F0FB6B05425ABA32D6C13A7BD22C" Version="1" BaseSiteTemplate="STS#0">  
  6.             <pnp:Files>  
  7.                 <pnp:Directory Src="Pics\" Folder="CustomLibrary" Overwrite="true" Recursive="true" Level="Published"></pnp:Directory>  
  8.             </pnp:Files>  
  9.         </pnp:ProvisioningTemplate>  
  10.     </pnp:Templates>  
  11. </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.

SharePoint 

Folder Structure in Windows

In the schema, I mentioned the Source folder as “Pics” in Src of Pnp:Directory node.

  1. <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”.

SharePoint
 
SharePoint 

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.

SharePoint 

Then, create your library in the SharePoint Site. I created a Document Library and named it as "CustomLibrary".

SharePoint 
 
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.
 
SharePoint 

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now