Provisioning of the managed metadata during creation of site collection is quite often requirement in Sharepoint development. It is common case when with site collection or web site creation you need to provision some initial content in order to simplify work of the content producers (e.g. content pages, lookup lists with data, etc). Metadata also can be provisioned during site collection creation via feature activation. E.g. customers may provide you default metadata in xml file and you need to create taxonomy based on this file. In this post I will show how to do it.
First of all we need to create a feature itself. In order to do this we need to determine the scope of our feature. As we want to provision managed metadata with site collection creation our feature will have Site scope. It is convenient because we can add it into onet.xml of custom site definitions (into <SiteFeatures> section of appropriate template). But you also need to note that content producers may change the metadata after it was provisioned. The simplest solution which can be used in order to prevent overriding of the user changes is to check that metadata is not provisioned yet (e.g. check that managed metadata group doesn’t exist). More complicated solution is to support merge of the term sets and terms. In my example simple all or nothing approach will be used.
Now let’s return to our feature. Here its feature.xml file:
It has feature receiver which will make actual work, i.e. it will parse xml file and create taxonomy in your Managed metadata service application. Also it has reference to the DefaultMetadata.xml file which contains default taxonomy. E.g. default metadata for the internet shop may look like this (for simplicity I use only couple of terms):
Although this example xml file contains one term set – code shown below will also work for multiple term sets case. Provisioning of this metadata will be performed in 2 steps:
- Parse xml and store data in object representation
- Create term sets and terms
We will need 2 helper DTO classes:
As you can see these are simple representation of OTB TermSet and Term entities.
Now let’s start from the code of the feature receiver:
Very important moment: the code is executed under elevated privileges. It means that you will need to add account of the application pool under which this code is executed (if you create site collection via UI in Central Administration – it will be account of the CA application pool) to the Term store administrators in Managed metadata service application.
The code of the feature receiver is quite simple – it passes path to the DefaultMetadata.xml file to the TermSetsService instance which makes all actual work. It is another useful pattern – decouple functionality from Sharepoint artifacts:
The code is quite obvious: at first it recursively parses xml file and loads result to the DTO objects defined above (see load() and getTerms() methods). Then it creates taxonomy in Term store using object model (see createTermSets() and createTerms() methods) – also recursively (you need to add reference to the Micosoft.SharePoint.Taxonomy.dll assembly in order to compile this code).
Also in order to compile it – you will need to add extension method IsNullOrEmpty() for IEnumerable<T> described by Phil Haack in his blog post.
This code uses English local (lcid = 1033) for default term sets. But you can extend it in order to provide translations for your terms. Also it can be extended in order to add support of the synonyms provisioning.
When you will deploy wsp and activate feature in your site collection – it will create hierarchical taxonomy in the Term store:
That’s how you can provision taxonomy with site collection creation. Hope it will help you in your work.