Importing a termset from XML file
Our XML file will have the following structure:
First of all add the SharePoint Shell into the script:
Then create a SharePoint Taxonomy session, and open the termstore on the target site collection :
Next open the XML file for reading using the Get-Content command
Now you can loop round each of the nodes in the XML document, first of the groups (folder in termstore manager). Determine if it is present already in the termstore and add if necessary:
Whilst in the group loop we can setup the termsets within that group. Again test to see if the termset exists and create it if not.
N.B. The method for creating a termset is similar to that of a group however this time we also specify the GUID for the termset. The reason for this is, if you create a Visual Studio SharePoint package with a Managed Metadata column (either by hand writing it or extracting from a WSP) part of the definition is the termset GUID, ergo when you create the termset it needs to have the same GUID as in the SharePoint solution.
Whilst in the termsets loop we can setup the Terms within that termset. Again test to see if the term exists and create it if not.
N.B. Again we specify the new items (in this case the term) GUID. This is for the same reason as above and also items which use the Managed Metadata column only store the GUID of the term, therefore the GUID’s need to match. Other properties can be set at the same time, the example below sets whether the term is available for tagging.
Do the same for child terms whilst in the terms loop.
Next close all the loops and commit the changes to the termstore (if you do not do this no changes will be made).
Finally SharePoint stores a copy of the termstore in a hidden list called “TaxonomyHiddenList” in the site. In order to update the termstore you need to tell SharePoint to resync the hidden list:
However this may not always work, if you are restore a content database onto a site then the hidden list also comes across (as you would aspect). Take a look at the hidden list’s field (if needs be look in SharePoint designer):
- Term1033 (or Locale)
The offending item here is the IdForTermStore, this stores the Term Store ID which is unique to the farm and cannot be set through Powershell nor can it be omitted in the Visual Studio field definition. So what you need to do is write a piece of script to get all the items in this hidden list and update the IdForTermStore with the ID of the TermStore.Id, after resync the list and all items which use the term store should now have valid up to date content.