This topic has not yet been rated - Rate this topic

TaxonomySession Class

SharePoint 2010

Wraps up all of the associated TermStore objects for a SPSite object.

System.Object
  Microsoft.SharePoint.Taxonomy.TaxonomySession

Namespace:  Microsoft.SharePoint.Taxonomy
Assembly:  Microsoft.SharePoint.Taxonomy (in Microsoft.SharePoint.Taxonomy.dll)
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public sealed class TaxonomySession
using System;
using System.IO;
using System.Globalization;
using System.Collections.Specialized;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace Microsoft.SDK.SharePointServer.Samples
{
    public static class TaxonomySamples
    {
        public static void UseSession(SPSite site)
        {
            TaxonomySession session = new TaxonomySession(site);

            // Get the default keyword TermStore for the provided site
            TermStore defaultKeywordStore = session.DefaultKeywordsTermStore;
            if (defaultKeywordStore != null)
            {
                Console.WriteLine(defaultKeywordStore.Name);
            }
            else
            {
                Console.WriteLine("Default keyword store is not configured or not configured properly");
            }

            // Get the default site collection TermStore associated with the provide site.
            TermStore defaultSiteCollectionStore = session.DefaultSiteCollectionTermStore;
            if (defaultSiteCollectionStore != null)
            {
                Console.WriteLine(defaultSiteCollectionStore.Name);
            }
            else
            {
                Console.WriteLine("Default site collection TermStore is not configured or not configured properly");
            }

            // Get all the TermStores associated with the provided site.
            TermStoreCollection termStores = session.TermStores;
            Console.WriteLine(termStores.Count);

            // Get all the offline TermStore names
            StringCollection names = session.OfflineTermStoreNames;
            Console.WriteLine(names.Count);

            // Resync the taxonomy hidden list to make sure it is update-to-date
            TaxonomySession.SyncHiddenList(site);
        }

        public static void RetrieveTerm(SPSite site, Guid termId)
        {
            TaxonomySession session = new TaxonomySession(site);

            // Retrieves a Term with the provided Id
            Term term = session.GetTerm(termId);
            Console.WriteLine("Got Term " + term.Name);
        }

        public static void SearchTermsByLabel(SPSite site, string prefix)
        {
            TaxonomySession session = new TaxonomySession(site);

            // Search all Terms that start with the provide prefix from
            // all TermStores associated with the provided site.
            TermCollection terms = session.GetTerms(prefix,
                true, // Only search in default labels
                StringMatchOption.StartsWith,
                5,  // The maximum number of terms returned from each TermStore
                true); // The results should not contain unavailable terms

            Console.WriteLine("The number of matching Terms is " + terms.Count);
        }


        public static void SearchTermsByCustomProperty(SPSite site, 
            string customPropertyName)
        {
            TaxonomySession session = new TaxonomySession(site);

            // Search all Terms that contain a custom property with the provided name
            // from all TermStores associated with the provided site.
            TermCollection terms = session.GetTermsWithCustomProperty(
                customPropertyName,
                true); // The results should not contain unavailable Terms

            Console.WriteLine("The number of matching Terms is " + terms.Count);
        }

        public static void SearchTermSetsByName(SPSite site,
            string termSetName)
        {
            TaxonomySession session = new TaxonomySession(site);

            // Search all TermSets that are using the provided name in current
            // UI LCID from all TermStores associated with the provided site.
            TermSetCollection termSets = session.GetTermSets(termSetName,
                CultureInfo.CurrentUICulture.LCID);

            Console.WriteLine("The number of matching Term Sets is " + termSets.Count);
        }

        public static void SearchTermSetsByTermLabels(SPSite site,
            string[] termLabels)
        {
            TaxonomySession session = new TaxonomySession(site);

            // Returns all TermSet instances from all TermStores that contain terms 
            // with matching labels for all specified strings.
            TermSetCollection termSets = session.GetTermSets(termLabels);
            Console.WriteLine("The number of matching Term Sets is " + termSets.Count);
        }

    }
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Getting the Taxonomy exception: TermStore Operation Error. Failed to read from or write to database
The complete Message reads:
- TermStore Operation Error.  Failed to read from or write to database. Refresh and try again. If the problem persists, please contact the administrator.

This happens within a SharePoint Timer Job that imports a Taxonomy from an outside source. The first time it commits the Terms it words fine, but afterwards I keep getting this exception. I'm also thinking is it related to the TaxonomySession.
The workaround right now is to restart the SharePoint Timer on Windows Services. Since TaxonomySession does not implement IDisposable, I do not know how to correctly handle the end of a TaxonomySession.

Microsoft.SharePoint.Taxonomy.TermStoreOperationException: Failed to read from or write to database.

I am getting this exception when trying to create TermStores and Terms from a pre-exported XML file in my Opensource Termstore tool (http://taxomatic.codeplex.com) :
"Microsoft.SharePoint.Taxonomy.TermStoreOperationException: Failed to read from or write to database. Refresh and try again."

So far, I have not been able to find a property to control the database timeout value and have tried various workaround, including creating a new taxonomy session in my loop to putting in a delay to allow the DB to catchup. Nothing has worked so far.

This issue is discussed at: http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/f6b721dc-139b-4742-a1f3-0e03cdee2656/

and even more

I'm using the code below to get the group, termset etc.
$url = "http://myserver/sites/sampleSC/projects/Project01"
$web = Get-SPWeb $url
$taxonomySession = Get-SPTaxonomySession -Site $web.Site
$termStore = $taxonomySession.TermStores["Managed Metadata Service"];
foreach($item in $termStore.Groups){
if($item.Name -eq "myGroupName")
{
$group = $item
}
}
Write-Host "---->: " $group.Name
$termset = $group.TermSets["the Term"];
foreach($t in $termset.Terms){
Write-Host $t.Name ":" $t.Id
}
PowerShell CMDLET: Get-SPTaxonomySession

Using the SharePoint 2010 Management Shell, a TaxonomySession object can be obtained using the Get-SPTaxonomySession CMDLET.

Example:

$taxonomySession = Get-SPTaxonomySession -Site "http://yoursitecollectionaddress"