Search code examples
c#powershellsharepoint-2013taxonomy

How to Call Sharepoint Taxonomy in C#


I have the following bit of PowerShell, which returns a list of all the Departments stored in SharePoint's managed metadata :

Add-PSSnapin Microsoft.Sharepoint.Powershell

# Get the site collection
$sitecollection = Get-SPSite 'http://mysharepointsite/'

# Get the term store id
$taxsession = Get-SPTaxonomySession -Site $sitecollection

# Change to the requierd service name
$termstore = $taxsession.TermStores["Managed Metadata Service"] 
$termstore.id

# Get the term set id
$termstoregroup = $termstore.Groups["People"]
# Change to your term set name
$termset = $termstoregroup.TermSets["Department"]
$termset.id

$departments = $termset.id.name

I need to achieve the same thing using C#, but I can't find anything that works for me. Does anyone know what the standard approach would be here?

I could always begin a PowerShell session from inside my C# app, but this seems like a very roundabout way of doing something which C# should have no problem with.


Solution

  • I managed to do this from C# using the following code :

    var spSite = new SPSite(@"http://mysharepointsite/");
    
    var taxSession = new TaxonomySession(spSite);
    
    var termStore = taxSession.TermStores["Managed Metadata Service"];
    var termStoreGroup = termStore.Groups["People"];
    var termSet = termStoreGroup.TermSets["Department"];
    
    var deps = termSet.Terms;
    
    foreach (var dep in deps)
    {
        MessageBox.Show(dep.Name);
    }
    

    Which is almost identical to that in the main question.

    It is important to not that this code will only run on the server which has SharePoint installed on it. When running this on my regular dev machine I get this exception on line 1 :

    An unhandled exception of type 'System.TypeInitializationException' occurred in Microsoft.SharePoint.dll

    Additional information: The type initializer for 'Microsoft.SharePoint.CoreResource' threw an exception.

    Similarly, I can only run the PowerShell script from the question from the SharePoint server - which makes sense as these appear to be using the same .Net object, i.e. TaxonomySession


    Using this sample, I was also able to retrieve the list of departments from my own machine with the following code :

    var clientContext = new ClientContext("http://mysharepointsite/")
        { AuthenticationMode = ClientAuthenticationMode.Default};
    
    var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
    var termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
    clientContext.Load(termStore,
            store => store.Name,
            store => store.Groups.Include(
                group => group.Name,
                group => group.TermSets.Include(
                    termSet => termSet.Name,
                    termSet => termSet.Terms.Include(
                        term => term.Name)
                )
            )
    );
    clientContext.ExecuteQuery();
    
    if (taxonomySession != null)
    {
        if (termStore != null)
        {
            foreach (var termGroup in termStore.Groups)
            {
                foreach (var termSet in termGroup.TermSets)
                {
                    foreach (var term in termSet.Terms)
                    {
                        MessageBox.Show(term.Name);
                    }
                }
            }
        }
    }
    

    As Murray Foxcroft mentioned, this makes use of the Client Side Object Model (CSOM), which allows us to access the taxonomy remotely.