SharePoint Online: Export-Import Site Collection Term Groups using PowerShell

Requirement: Export and Import Site Collection Term Groups between SharePoint Online Sites.

PowerShell to Export-Import Site Collection Term Groups in SharePoint Online:
Managed metadata created in tenant term store can be accessed from any SharePoint Online site in the Office 365 tenant. However, Term sets created locally in a site collection is limited within its site collection and can't be accessed beyond its site collection boundary. As there are no ways to export local term sets from a site collection, we can use PowerShell script to copy-paste term groups in SharePoint Online.
powershell to export import site collection term groups

Export Site Collection Term Groups using PowerShell
Just set the parameters w.r.t your environment and run the script to export a site collection terms groups to a XML file.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$FilePath = "C:\Temp\TermGroup.xml" 

#Connect to Site 
Connect-PnPOnline $SiteURL -UseWebLogin

#Export Site Collection's Term Groups to XML
Export-PnPTermGroupToXml -Out $FilePath -Identity "{sitecollectiontermgroupname}"

The exported XML file would look like:
<pnp:TermGroups xmlns:pnp="http://schemas.dev.office.com/PnP/2020/02/ProvisioningSchema">
  <pnp:TermGroup Name="{sitecollectiontermgroupname}" ID="00000000-0000-0000-0000-000000000000" Description="" SiteCollectionTermGroup="true" UpdateBehavior="Overwrite">
    <pnp:TermSets>
      <pnp:TermSet Name="Classification" ID="00000000-0000-0000-0000-000000000000" Description="" IsOpenForTermCreation="true" />
      <pnp:TermSet Name="Project Classification" ID="00000000-0000-0000-0000-000000000000" Description="" IsOpenForTermCreation="true">
        <pnp:Terms>
          <pnp:Term Name="Development" ID="00000000-0000-0000-0000-000000000000" Owner="i:0#.f|membership|[email protected]" Description="" SourceTermId="1fbcc5b0-9ee7-4016-8e90-4dc29df0e602">
            <pnp:Terms />
          </pnp:Term>
          <pnp:Term Name="Finance" ID="00000000-0000-0000-0000-000000000000" Owner="i:0#.f|membership|[email protected]" Description="" SourceTermId="0540c6bf-d766-4629-9092-0533d1ebfbb7">
            <pnp:Terms />
          </pnp:Term>
          <pnp:Term Name="HR" ID="00000000-0000-0000-0000-000000000000" Owner="i:0#.f|membership|[email protected]" Description="" SourceTermId="e59e7042-8395-4dba-9d40-7b07af110307">
            <pnp:Terms />
          </pnp:Term>
          <pnp:Term Name="Infrastructure" ID="00000000-0000-0000-0000-000000000000" Owner="i:0#.f|membership|[email protected]" Description="" SourceTermId="d31ff7c3-29a6-438c-b751-b28758a767ed">
            <pnp:Terms />
          </pnp:Term>
          <pnp:Term Name="IT" ID="00000000-0000-0000-0000-000000000000" Owner="i:0#.f|membership|[email protected]" Description="" SourceTermId="5d92346c-45e9-4113-89d7-63c88cdf2680">
            <pnp:Terms />
          </pnp:Term>
          <pnp:Term Name="Management" ID="00000000-0000-0000-0000-000000000000" Owner="i:0#.f|membership|[email protected]" Description="" SourceTermId="d48797a4-9055-4d39-b78d-e45178b571b2">
            <pnp:Terms />
          </pnp:Term>
          <pnp:Term Name="Marketing" ID="00000000-0000-0000-0000-000000000000" Owner="i:0#.f|membership|[email protected]" Description="" SourceTermId="395ce170-a53b-4e0a-9a63-caa444e5b73e">
            <pnp:Terms />
          </pnp:Term>
          <pnp:Term Name="Operations" ID="00000000-0000-0000-0000-000000000000" Owner="i:0#.f|membership|[email protected]" Description="" SourceTermId="2c1ac652-aeb3-4418-8014-f834858713d9">
            <pnp:Terms />
          </pnp:Term>
          <pnp:Term Name="Warehouse" ID="00000000-0000-0000-0000-000000000000" Owner="i:0#.f|membership|[email protected]" Description="" SourceTermId="85ca4a71-0c6c-4974-89a1-ad413f19e251">
            <pnp:Terms />
          </pnp:Term>
        </pnp:Terms>
      </pnp:TermSet>
      <pnp:TermSet Name="Regions" ID="00000000-0000-0000-0000-000000000000" Description="" IsOpenForTermCreation="true">
        <pnp:Terms>
          <pnp:Term Name="APAC" ID="00000000-0000-0000-0000-000000000000" Owner="i:0#.f|membership|[email protected]" Description="" SourceTermId="dadcb21f-1e86-413c-8e58-71dd899d9ac7">
            <pnp:Terms />
          </pnp:Term>
          <pnp:Term Name="EMEA" ID="00000000-0000-0000-0000-000000000000" Owner="i:0#.f|membership|[email protected]" Description="" SourceTermId="77ea30de-778f-467a-a639-029cddc89ea2">
            <pnp:Terms />
          </pnp:Term>
          <pnp:Term Name="MENA" ID="00000000-0000-0000-0000-000000000000" Owner="i:0#.f|membership|[email protected]" Description="" SourceTermId="f561a115-9261-4f5e-8c64-312ea7cf05e9">
            <pnp:Terms />
          </pnp:Term>
          <pnp:Term Name="NA" ID="00000000-0000-0000-0000-000000000000" Owner="i:0#.f|membership|[email protected]" Description="" SourceTermId="956a35a9-4765-4824-ad56-2dbaf9abf075">
            <pnp:Terms />
          </pnp:Term>
        </pnp:Terms>
      </pnp:TermSet>
    </pnp:TermSets>
  </pnp:TermGroup>
</pnp:TermGroups>

Import Site Collection Term Groups
Once we have the term groups XML ready, we can import it to any other site collection.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Sales"
$FilePath = "C:\Temp\TermGroup.xml" 

#Connect to Site 
Connect-PnPOnline $SiteURL -UseWebLogin

#Import Term Group from XML file
Import-PnPTermGroupFromXml -Path $FilePath

Similarly, you can export-import any term group from termstore between tenants! E.g.
Get-PnPTermGroup -Identity "PMO" | Export-PnPTermGroupToXml -Out "C:\Temp\TG.xml"

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.