Move Site Columns From A SharePoint Site To Another Using PNP PowerShell

This is about moving site columns or import/export site columns from one site to another.

Using PNP, we can retrieve the complete backup of a site but just to move the site columns from one to another, we need to write custom PS scripts. In this article, I am picking a Site Column group and provisioning it to another site using PNP PowerShell.

Before proceeding, we need to ensure that the SharePoint Online Management Shell is installed on your machine. If not, use this link to download and install it. Also, we have to make sure that we have enough permissions to retrieve the field schema from the source site and to provision them in the target site.

If we are provisioning inside the same tenant, there won't be much trouble in provisioning taxonomy fields. If we need to provision the taxonomy fields in a different tenant, we should have term store related permissions. Otherwise, we are not able to create term sets which, in turn, affect the PNP provisioning process.

I am considering PSSamples site from my tenant as source and will proceed to move the site columns from a Site Column group called “My columns”. I will get all the site columns that come under this group, using the below PS script and frame it as a PNP provisioning XML.

  1. Connect-PnPOnline -Url "https://**********.sharepoint.com/sites/PSSamples"  
  2. $flds= Get-PnPField -Group "My Columns"  
  3. $fldsSchema='<?xml version="1.0"?>  
  4. <pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2018/01/ProvisioningSchema">  
  5. <pnp:Preferences Generator="OfficeDevPnP.Core, Version=2.23.1802.0, Culture=neutral, PublicKeyToken=5e633289e95c321a" />  
  6. <pnp:Templates ID="CONTAINER-TEMPLATE-0F2053A13C224D369E82C2FA2EC4A736">  
  7. <pnp:ProvisioningTemplate ID="TEMPLATE-0F2053A13C224D369E82C2FA2EC4A736" Version="1" BaseSiteTemplate="STS#0">  
  8. <pnp:SiteFields>'  
  9. ForEach($fld in $flds){  
  10. $fldsSchema+=$fld.SchemaXml  
  11. }  
  12. $fldsSchema+='</pnp:SiteFields> </pnp:ProvisioningTemplate>  
  13. </pnp:Templates>  
  14. </pnp:Provisioning>'  
  15. $fldsSchema 
I enclosed the schema of the XML inside the PNP Provisioning schema format which was released on Jan 2018, so that we can provision the result XML to a target site using PNP core libraries. After running the above code, we will be getting an XML like below.
  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.23.1802.0, Culture=neutral, PublicKeyToken=5e633289e95c321a" />  
  4.     <pnp:Templates ID="CONTAINER-TEMPLATE-0F2053A13C224D369E82C2FA2EC4A736">  
  5.         <pnp:ProvisioningTemplate ID="TEMPLATE-0F2053A13C224D369E82C2FA2EC4A736" Version="1" BaseSiteTemplate="STS#0">  
  6.             <pnp:SiteFields>  
  7.                 <Field Type="Note" DisplayName="Test Column 3" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" NumLines="6" RichText="TRUE" RichTextMode="FullHtml" IsolateStyles="TRUE" Sortable="FALSE" Group="My Columns" ID="{321e4062-e9a3-4ea3-9a91-62b55e237ce1}" SourceID="{22ad7aad-8c48-4a68-a747-7f7fc2470fda}" StaticName="Test_x0020_Column_x0020_3" Name="Test_x0020_Column_x0020_3" Version="1"></Field>  
  8.                 <Field Type="Text" DisplayName="Test Column1" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" MaxLength="255" Group="My Columns" ID="{8dbafd95-7e25-4866-ba68-eecb55217b2a}" SourceID="{22ad7aad-8c48-4a68-a747-7f7fc2470fda}" StaticName="Test_x0020_Column1" Name="Test_x0020_Column1" Version="1"></Field>  
  9.                 <Field Type="Text" DisplayName="Test COlumn2" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" MaxLength="255" Group="My Columns" ID="{a9638cdf-16d3-43c4-be8e-a3f434c08ae6}" SourceID="{22ad7aad-8c48-4a68-a747-7f7fc2470fda}" StaticName="Test_x0020_COlumn2" Name="Test_x0020_COlumn2" Version="1"></Field>  
  10.             </pnp:SiteFields>  
  11.         </pnp:ProvisioningTemplate>  
  12.     </pnp:Templates>  
  13. </pnp:Provisioning>  
Save the XML in a separate file and run the below script to provision it to a target site.
  1. Connect-PnPOnline -Url "https://*********.sharepoint.com/sites/content1"  
  2. Apply-PnPProvisioningTemplate -Path "C:\Users\ADMIN\Desktop\fields.xml"  
PowerShell will show some interactive progress bar where we can see the field by field updates. If needed, we can log that the process has been running in the background. After provisioning, go to the target site's site settings and then go to Site Columns page to see the changes made to the SharePoint site using PNP provisioning.

 

SharePoint