3 min read
DevelopmentPowerShellSharePoint

SharePoint 2010: Accessing the Managed Metadata Service via Code

I was recently asked to provide some code which could query the managed metadata service of a SharePoint 2010 implementation and extract the terms for use in a separate web site.

This turned out to be a bit more complicated than I initially thought as the architecture called for use of the SharePoint Web Services, as opposed to using the client object model, however as the code show below indicates if you want to use the web service to access the term store you need to now the ID of both the term store and the term set that you wish to interrogate. The problem with this is that this is almost certainly to be different on the various environments where this may be used, additionally it is possible that you may wish to change the term store being used.

Anyway to hook into the web service you will need to add a reference to the TaxonomyClientService.asmx service, note that this is a asmx web service not a wcf service, so when adding a service reference remember to go to the advanced settings and add this as a web reference as opposed to a service reference.

The other aspect of this code which is not perhaps clear is the timestamp field, but you should be ok to use the same timestamp value as shown here. Please also not the term store id and term set id, these need to be replaced with the ids for the SharePoint implementation you are querying.

To do this use PowerShell and run the following PowerShell script.

#Get the Site Collection
$SiteCollection = Get-SPSite http://%5BSharePoint Site] #Change to you site URL

#Get the Term Store ID
$TaxSession Get-SPTaxonomySession -Site $SiteCollection
$TermStore = $TaxSession.TermStores[“Managed Metadata Service”] #Change to your service name
$TermStore.Id

#Get the Term Set ID
$TermStoreGroup = $TermStore.Groups[“Corporate Taxonomy”] #Change to your Terms Store name
$TermSet = $TermStoreGroup.TermSets[“Department”] #Change to your term set name
$TermSet.Id

Alternatively you could query a document library or list which is associated with this term store, but if this does not have a reference to the correct term set, then this may not bring back all the id’s you need.

Once the relevant ID’s have been retrieved you can then use the following code to call the web service.

//Add reference to sharepoint taxonomy web service: url = http://[site_Name] /_vti_bin/TaxonomyClientService.asmx
using (Taxonomywebservice svc = new Taxonomywebservice())
{
    svc.Credentials = System.Net.CredentialCache.DefaultCredentials;
string oldTimeStamp = “<timeStamp>633992461437070000</timeStamp>”;
    string timeStamp = “”;
    string clientVersion = “<version>1</version>”;
    string termStoreIds = “<termStoreId>1b490cb5-7fee-41d9-b0ec-0b8d87fdb051</termStoreId>”//Replace with your Terms Store ID
    string termSetIds = “<termSetId>1e92d7c9-dc1e-4720-b4e7-791ab7493cd2</termSetId>”//Replace with your term Set ID
    int lcid = 1033; //Replace with your term Set ID
    try
    {
        //The results are returned in xml
        string results = svc.GetTermSet(termStoreIds, termSetIds, lcid, oldTimeStamp, clientVersion, out timeStamp);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

An alternative method is to use the client object model, the below code sample show how this might be achieved. In my opinion this code is a lot more intuitive, as you only need to provide the names of the term store and sets, but the method used should be driven by the solution architecture stipulated.

using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint;

using (SPSite oSiteCollection = new SPSite(“Site URL”))  //Replace with site URL
 
    TaxonomySession session = new TaxonomySession(oSiteCollection);     TermStore termStore = session.DefaultKeywordsTermStore;     Group group = termStore.Groups["Term_Store"];  //Replace with term store name     TermSet termSet = group.TermSets["Term_Set"]; //Replace with term set name

     foreach (var topTerm in termSet.Terms)
    {
       //Do Something
       foreach (var childTerm in topTerm.Terms)
       {
           //Do Something

       }
    }
}

Other useful posts about working with the managed metadata service programmatically.

http://www.insidesharepoint.net/post/Using-taxonomy-fields-in-Sharepoint-2010-Part-II.aspx

http://blogs.solidq.com/sharepoint/Post.aspx?ID=37&title=sharepoint+2010%2C+managed+metadata%2C+taxonomyclientservice+in+depth

Want more cloud insights? Listen to Cloudy with a Chance of Insights podcast:

Spotify | YouTube | Apple Podcasts

Tags

Developmentmanaged meta data services
Like this article?

Comments

Richard Hogan

Richard Hogan

Author & Host

Richard is a Microsoft-focused architect and consultant with deep expertise in Azure, Microsoft 365, cybersecurity, and enterprise cloud migration. He is the founder of The Microsoft Cloud Blog and co-host of the Cloudy with a Chance of Insights podcast. All views expressed are his own.

You might also like

SharePoint 2010: PowerShell script to retreive Managed Meta Data Service and Terms Store IDs

This script has already been produced in a previous post, but for convenience I thought I would replicate it here. #Get the Site Collection $SiteCollection = Get-SPSite http://%5BSharePoint Site] #Change to you site URL #Get the Term Store ID $TaxSession = Get-SPTaxonomySession -Site $SiteCollection $TermStore = $TaxSession.TermStores[&#8220;Managed Metadata Service&#8221;] #Change to your service name $TermStore.Id&#8230; Continue Reading &rarr;

Error Uploading Solution to SharePoint Solution Gallery

If when you upload a solution to the solution gallery in SharePoint 2010 you receive the following error. &#8220;This solution contains invalid markup or elements that cannot be deployed as part of a sandboxed solution. Solution manifest for solution &#8217;64afe4f8-6a99-42e8-9845-43fc18cc49f8&#8242; failed validation, file manifest.xml, line 10, character 4: The element &#8216;Solution&#8217; in namespace &#8216;http://schemas.microsoft.com/sharepoint/&#038;#8217; has invalid&#8230; Continue Reading &rarr;

SharePoint 2013: PowerShell to update a term sets Submission Policy

The following PowerShell can be used to update a terms sets submission policy to open or closed. #Connect to Central Admin $Session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession(&#8220;Central Admin URL&#8221;) #Connect to Managed Metadata Service $Store = $Session.TermStores[&#8220;Managed Metadata Service Name&#8221;] #Get the group $Group = $Store.Groups[&#8220;Group Name&#8221;]; #Get the Term Set $TermSet = $Group.TermSets[&#8220;Term Set Name&#8221;]; #Update&#8230; Continue Reading &rarr;

In-depth cloud tech discussions from Microsoft experts.

Expert insights on Microsoft Azure, cloud architecture, and enterprise technology.