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.
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.
Connect-PnPOnline -Url “https://**********.sharepoint.com/sites/PSSamples”
$flds= Get-PnPField -Group “My Columns”
$fldsSchema='<?xml version=“1.0”?>
<pnp:Provisioning >”http://schemas.dev.office.com/PnP/2018/01/ProvisioningSchema”>
<pnp:Preferences Generator=“OfficeDevPnP.Core, Version=2.23.1802.0, Culture=neutral, PublicKeyToken=5e633289e95c321a” />
<pnp:Templates ID=“CONTAINER-TEMPLATE-0F2053A13C224D369E82C2FA2EC4A736”>
<pnp:ProvisioningTemplate ID=“TEMPLATE-0F2053A13C224D369E82C2FA2EC4A736” Version=“1” BaseSiteTemplate=“STS#0”>
<pnp:SiteFields>’
ForEach($fld in $flds){
$fldsSchema+=$fld.SchemaXml
}
$fldsSchema+='</pnp:SiteFields> </pnp:ProvisioningTemplate>
</pnp:Templates>
</pnp:Provisioning>’
$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.
<?xml version=“1.0”?>
<pnp:Provisioning >”http://schemas.dev.office.com/PnP/2018/01/ProvisioningSchema”>
<pnp:Preferences Generator=“OfficeDevPnP.Core, Version=2.23.1802.0, Culture=neutral, PublicKeyToken=5e633289e95c321a” />
<pnp:Templates ID=“CONTAINER-TEMPLATE-0F2053A13C224D369E82C2FA2EC4A736”>
<pnp:ProvisioningTemplate ID=“TEMPLATE-0F2053A13C224D369E82C2FA2EC4A736” Version=“1” BaseSiteTemplate=“STS#0”>
<pnp:SiteFields>
<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>
<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>
<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>
</pnp:SiteFields>
</pnp:ProvisioningTemplate>
</pnp:Templates>
</pnp:Provisioning>
