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
Woh I like your posts , saved to fav! .
LikeLike
You are my hero. Thank you for this.
LikeLike