Exporting and Importing SharePoint 2010 Terms

Posted on Posted in PowerShell Cmdlets, SharePoint 2010

Ever had the need to migrate Terms from the Managed Metadata Term Store from one environment to another? Do you find the flat, CSV, import approach provided out of the box to be insufficient (especially with its lack of support for alternate labels)? When I first started working with Terms over two years ago I was extremely frustrated by the lack of export and import capabilities so I decided to solve the problem myself by creating two cmdlets, Export-SPTerms and Import-SPTerms. I’ve actually had these cmdlets publicly available for about two years now but I suspect very few people actually know they exist so I thought I’d put together this short post just to highlight them (I also recently pushed out an update which adds support for Site Collection scoped Groups if you have SP1 deployed).

I don’t want to go into a lot of detail regarding how these cmdlets work as they’re really very simple and the bulk of the code is just about iterating through the Term Store structure and turning the various objects into an XML structure (you can download the code from my downloads page). So with brevity in mind, here’s the full help for the Export-SPTerms cmdlet:

NAME
    Export-SPTerms
   
SYNOPSIS
    Export the Managed Metadata Terms.
   
SYNTAX
    Export-SPTerms [-TaxonomySession] <SPTaxonomySessionPipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Export-SPTerms [-TermStore] <SPTaxonomyTermStorePipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Export-SPTerms [-Group] <SPTaxonomyGroupPipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Export-SPTerms [-TermSet] <SPTaxonomyTermSetPipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Export-SPTerms [-Term] <SPTaxonomyTermPipeBind> [[-OutputFile] <String>] [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
   
DESCRIPTION
    Export the Managed Metadata Terms.
   
    Copyright 2011 Falchion Consulting, LLC
    > For more information on this cmdlet and others:
    >http://blog.falchionconsulting.com/
    > Use of this cmdlet is at your own risk.
    > Gary Lapointe assumes no liability.
   

PARAMETERS
    -TaxonomySession <SPTaxonomySessionPipeBind>
        The TaxonomySession object containing the Term Stores to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    -TermStore <SPTaxonomyTermStorePipeBind>
        The TermStore object containing the terms to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -Group <SPTaxonomyGroupPipeBind>
        The Group object containing the terms to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -TermSet <SPTaxonomyTermSetPipeBind>
        The TermSet object containing the terms to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -Term <SPTaxonomyTermPipeBind>
        The Term object containing the terms to export.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -OutputFile [<String>]
        The path to the file to save the terms to.
       
        Required?                    false
        Position?                    2
        Default value               
        Accept pipeline input?       false
        Accept wildcard characters?  false
       
    -AssignmentCollection [<SPAssignmentCollection>]
        Manages objects for the purpose of proper disposal. Use of objects, such as SPWeb or SPSite, can use large amounts of memory and use of these objects in Windows PowerShell scripts requires proper memory management. Using the SPAssignment object, you can assign objects to a variable and dispose of the objects after they are needed to free up memory. When SPWeb, SPSite, or SPSiteAdministration objects are used, the objects are automatically disposed of if an assignment collection or the Global parameter is not used.
       
        When the Global parameter is used, all objects are contained in the global store. If objects are not immediately used, or disposed of by using the Stop-SPAssignment command, an out-of-memory scenario can occur.
       
        Required?                    false
        Position?                    named
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        "get-help about_commonparameters".
   
INPUTS
   
   
   
OUTPUTS
   
   
   
NOTES
   
   
    For more information, type "Get-Help Export-SPTerms -detailed". For technical information, type "Get-Help Export-SPTerms -full".
   
    ——————EXAMPLE 1———————–
   
    PS C:\> Export-SPTerms -TaxonomySession "http://site/" -OutputFile "c:\terms.xml"
   
   
    This example exports the terms for all term stores associated with the site and saves to c:\terms.xml.
   
    ——————EXAMPLE 2———————–
   
    PS C:\> Export-SPTerms -Group (Get-SPTaxonomySession -Site "http://site/").TermStores[0].Groups[0] -OutputFile "c:\terms.xml"
   
   
    This example exports the first Group of the first Term Store and saves to c:\terms.xml.
   
   
RELATED LINKS
    Import-SPTerms
    Get-SPTaxonomySession

 

Now lets look at the Import-SPTerms cmdlet (it’s very similar):

NAME
    Import-SPTerms
   
SYNOPSIS
    Import the Managed Metadata Terms.
   
SYNTAX
    Import-SPTerms [-TaxonomySession] <SPTaxonomySessionPipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Import-SPTerms [-ParentTermStore] <SPTaxonomyTermStorePipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Import-SPTerms [-ParentGroup] <SPTaxonomyGroupPipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Import-SPTerms [-ParentTermSet] <SPTaxonomyTermSetPipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
    Import-SPTerms [-ParentTerm] <SPTaxonomyTermPipeBind> [-InputFile] <XmlDocumentPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [<CommonParameters>]
   
   
DESCRIPTION
    Import the Managed Metadata Terms.
   
    Copyright 2011 Falchion Consulting, LLC
    > For more information on this cmdlet and others:
    > http://blog.falchionconsulting.com/
    > Use of this cmdlet is at your own risk.
    > Gary Lapointe assumes no liability.
   

PARAMETERS
    -TaxonomySession <SPTaxonomySessionPipeBind>
        The TaxonomySession object to import Term Stores into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    -ParentTermStore <SPTaxonomyTermStorePipeBind>
        The TermStore object to import Groups into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -ParentGroup <SPTaxonomyGroupPipeBind>
        The Group object to import Term Sets into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -ParentTermSet <SPTaxonomyTermSetPipeBind>
        The TermSet object to import Terms into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -ParentTerm <SPTaxonomyTermPipeBind>
        The Term object to import Terms into.
       
        Required?                    true
        Position?                    1
        Default value               
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
       
    -InputFile <XmlDocumentPipeBind>
        The path to the file containing the terms to import or an XmlDocument object or XML string.
       
        Required?                    true
        Position?                    2
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    -AssignmentCollection [<SPAssignmentCollection>]
        Manages objects for the purpose of proper disposal. Use of objects, such as SPWeb or SPSite, can use large amounts of memory and use of these objects in Windows PowerShell scripts requires proper memory management. Using the SPAssignment object, you can assign objects to a variable and dispose of the objects after they are needed to free up memory. When SPWeb, SPSite, or SPSiteAdministration objects are used, the objects are automatically disposed of if an assignment collection or the Global parameter is not used.
       
        When the Global parameter is used, all objects are contained in the global store. If objects are not immediately used, or disposed of by using the Stop-SPAssignment command, an out-of-memory scenario can occur.
       
        Required?                    false
        Position?                    named
        Default value               
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  false
       
    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        "get-help about_commonparameters".
   
INPUTS
   
   
   
OUTPUTS
   
   
   
NOTES
   
   
    For more information, type "Get-Help Import-SPTerms -detailed". For technical information, type "Get-Help Import-SPTerms -full".
   
    ——————EXAMPLE 1———————–
   
    PS C:\> Import-SPTerms -ParentTermStore (Get-SPTaxonomySession -Site "http://site/").TermStores[0] -InputFile "c:\terms.xml"
   
   
    This example imports the Group from c:\terms.xml to the first Term Store.
   
    ——————EXAMPLE 2———————–
   
    PS C:\> Import-SPTerms -TaxonomySession "http://site/" -InputFile "c:\terms.xml"
   
   
    This example imports the terms from c:\terms.xml to the Term Store associated with http://site.
   
   
RELATED LINKS
    Export-SPTerms
    Get-SPTaxonomySession

 

Using these cmdlets I can easily export terms from one environment, such as production, to another environment, such as my test environment. This avoids the need to have backup and restore the database associated with the term store (which requires removing and re-provisioning the service application. And the way I’ve written the cmdlets all the Term Set, Group, and Term IDs remain consistent and, because the exported XML is easily modified, when it comes to Site Collection scoped Groups you can do a simple search and replace to change Site Collection URLs to match the new target (note that I first check for a Site Collection with the specified URL and if not found then I use the ID and if I still can’t locate the Site Collection then I create the Group as a standard shared Group).

-Gary

10 thoughts on “Exporting and Importing SharePoint 2010 Terms

  1. “I’ve actually had these cmdlets publicly available for about two years now” – yet I can’t find them in your downloads section. Where can I get the import/export-spweb commandlets?

    1. The cmdlets are part of my SharePoint 2010 Server Cmdlets WSP available on the downloads page (I don’t provide separate downloads for the cmdlets – they are all bundled in a single package).

  2. Thanks Gary,

    I came across a problem trying to keep term stores “synced.” There are GUI-based tools out there but you really can’t automate with them. This is just what I was looking for – how timely!

  3. Hey Gary. Love the import/export term commandlets. However, it seems to error out when I try to import “re-used” terms. It will throw an error about not being able to write to the database. Is that to be expected? Other than that, the commanlets work beautifully.

  4. Hi Garry,

    my special thank for this cmdlets. This helps me a lot in my current project.
    I found only one problem @ SP2013 SP1 Enterprise: not existing groups are always created with new GUIDs . Can you fix this ? We using German and English in our MMS. German is the default language.
    BTW: $temGroup=$termStore.CreateGroup(“Mygroup”,”5b0ad682-b1ee-4800-87d5-38b51b160f39″) f.e. working fine…

  5. Good morning from Germany!
    I’m trying to migrate a SiteCollection(!) TermGroup from my SharePoint 2013 DEV Environment to my STAGING Environment. Important is that we will not lose the Unique identifiers for all the Terms, TermSets and also for the Group.
    Each time when i tried to migrate it worked fine – the IDs of the Terms and TermSets are the same (nice!)
    But the ID for the Group will be changed to a new one. Thats not nice because the customer is using TermSets for Managed Navigation.

    I’m using this command for the Export of the SiteCollection Group:
    Export-SPTerms -Group (Get-SPTaxonomySession -Site “http://MySiteCollectionURL”).TermStores[0].Groups[0] -OutputFile “C:\terms.xml”

    For the Import i’m using this command:
    Import-SPTerms -ParentTermStore (Get-SPTaxonomySession -Site “http://MySiteCollectionURL”).TermStores[0] -InputFile “C:\terms.xml”

    Is there maybe a way to Export and Import the complete Group and the ID of the Group will not be changed?

    Thank you very much!
    Stephan

    1. Not without putting my cmdlets in an unsupported state (in otherwords, I can do it but it would require the use of reflection). The API that Microsoft exposed didn’t allow me to do that (at least for 2010 – when I migrated the code to 2013 I didn’t check to see if that was resolved as I was just doing a build migration and not an upgrade).

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA

*