When you work with Sharepoint managed metadata via object model you often need to write the code similar to the following:
Sometimes TaxonomySession.DefaultKeywordsTermStore property may return null, i.e. termstore variable on line 4 in the example above will be null. In this case you can’t do any operations with managed metadata programmatically. What are the reasons of this behavior and how to fix it?
First of all check that your web application has connection with managed metadata service in Central administration > Manage web applications > Service connections:
This connection may be part of default connections set as shown on the picture above or included into the custom set. It is important that it will have only one connection, on one of the projects we encountered with situation when TaxonomySession.DefaultKeywordsTermStore returned null when there were several managed metadata connections defined for single web application.
If connection is defined properly go to Manage service applications > select Managed metadata service connection and click Properties button on the ribbon. On the opened dialog ensure that option “This service application is the default storage location for Keywords”:
There is also another option “This service application is the default storage location for column specific term sets”, but it affects another property: TaxonomySession.DefaultSiteCollectionTermStore.
If this step also didn’t help, check permissions. Go to Manage service applications > select Managed metadata service application and click Permissions button on the ribbon:
Ensure that account under which you run the code has at least “Read access to term store” right.
In most cases described steps should fix problem with null TaxonomySession.DefaultKeywordsTermStore or TaxonomySession.DefaultSiteCollectionTermStore properties. If you found some other reasons which can cause this problem, please share them in comments.