During provisioning to Sharepoint Online we usually try to automate as much actions as possible. One of such actions is creation of managed metadata hierarchy (Group > Term sets > Terms). In on-premise installation with full access to the basic object model this is straightforward process nowdays, but in Sharepoint Online we need to use client object model instead. Before to start you should ensure that account under which code will run is added to Term store administrators group (in order to check it on the root site of your site collection go to Site settings > Term store management). Also we will need xml file with initial managed metadata hierarchy in the following format:
As shown in the example above ids for groups and term sets are explicitly specified. They will be needed for binding managed metadata fields – I will write about it in another article. Also in this example only 2 levels of nested terms are shown, but you may use any nesting level, just ensure that xml if formatted correctly.
PowerShell script which creates taxonomy hierarchy from the xml file in the local site collection term store is shown below:
It is quite self-descriptive: at first it creates group, then for each term set xml tag creates appropriate term set and then terms with sub terms. Function Provision-TermSets is called this way:
After executing go to Site settings > Term store management and check that your managed metadata hierarchy is successfully created.