Reading Term Values from the Managed Metadata Service

The DLL is “Microsoft.SharePoint.Taxonomy” but it refers to the Managed Metadata service, which includes the Taxonomy. The following code seems logical for writing out all the term values. However, that’s not the case. It’s just a start.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace MossDelight.Demos.ManagedMetadataConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSite site = new SPSite("http://demo9/sites/MMS");
            TaxonomySession txsn = new TaxonomySession(site);
            foreach (TermStore termstore in txsn.TermStores)
            {
                Console.WriteLine(">>> " + termstore.Name);

                foreach (Group group in termstore.Groups)
                {
                    Console.WriteLine("+ " + group.Name);

                    foreach (TermSet termset in group.TermSets)
                    {
                        Console.WriteLine("++ " + termset.Name);

                        foreach (Term term in termset.Terms)
                        {
                            Console.WriteLine("   - " + term.Name);
                        }
                    }
                }
            }
            Console.WriteLine("* * * END * * *");
            Console.ReadLine();
        }
    }
}

What we are missing is the fact that a term can contain terms. Thus, the MMS is heirarchical. We’ll have to keep this in mind when importing data into our MMS.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace MossDelight.Demos.ManagedMetadataConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSite site = new SPSite("http://demo9/sites/MMS");
            TaxonomySession txsn = new TaxonomySession(site);
            foreach (TermStore termstore in txsn.TermStores)
            {
                Console.WriteLine(">>> " + termstore.Name);

                foreach (Group group in termstore.Groups)
                {
                    Console.WriteLine("+ " + group.Name);

                    foreach (TermSet termset in group.TermSets)
                    {
                        Console.WriteLine("++ " + termset.Name);

                        foreach (Term term in termset.Terms)
                        {
                            WriteTerm(term,0);
                        }
                    }
                }
            }
            Console.WriteLine("* * * END * * *");
            Console.ReadLine();
        }

        private static void WriteTerm(Term term, int level) {

            Console.WriteLine("- ".PadLeft(level + 4, ' ') + term.Name);

            foreach (Term childterm in term.Terms)
            {
                WriteTerm(childterm, ++level);
            }
        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: