Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How do you get a TaxonomyField object from the Managed Property Title? Does anyone know a good article/tutorial on programatically working with taxonomies in Sharepoint 2010?

share|improve this question

2 Answers 2

Try this:

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


class Program
{
    static void Main(string[] args)
    {
        //Your site url
        string siteUrl = "http://spsite";

        using (SPSite site = new SPSite(siteUrl))
        using (SPWeb web = site.OpenWeb())
        {
            //metadata field name
            string mmField = "MetaDataField";
            //List name
            string listName = "Shared Documents";
            SPList list = web.Lists[listName];
            foreach (SPListItem item in list.Items)
            {
                TaxonomyFieldValue value =  item[mmField] as TaxonomyFieldValue;
                if (value != null)
                {
                    Console.WriteLine(value.Label);
                }
            }
        }   
    }
}
share|improve this answer

Creating a TaxonomyField goes something like this:

var site = SPContext.Current.Site;
var web = site.RootWeb;
var taxonomySession = new TaxonomySession(site);
var termStore = taxonomySession.TermStores[0];
var termGroup = termStore.Groups["My Group"];
var termSet = termGroup.TermSets["My TermSet"];

var taxonomyField = web.Fields.CreateNewField("TaxonomyFieldType", "My Tag Field") as TaxonomyField;
taxonomyField.SspId = termStore.Id;
taxonomyField.TermSetId = termSet.Id;
taxonomyField.AllowMultipleValues = true;
web.Fields.Add(taxonomyField);
web.Update();

If you're going to work with Managed Metadata the Andrew Connell's article series is almost a must.

To get a reference to the field just use:

var taxonomyField = web.Fields["My Tag Field"] as TaxonomyField;

To set the value in a list item ALWAY use one of the TaxonomyField.SetFieldValue overloads, NEVER just use item["My Tag Field"] = xxx. The TaxonomyField is implemented as a lookup into a hidden list and the term needs to be copied into this list when first used.

If you need to get an exising field based on TermSet name the you have to loop through the fields to find it:

var site = SPContext.Current.Site;
var web = site.RootWeb;
var taxonomySession = new TaxonomySession(site);
var termStore = taxonomySession.TermStores[0];
var termGroup = termStore.Groups["My Group"];
var termSet = termGroup.TermSets["My TermSet"];

foreach (var field in web.Fields)
{
  var taxonomyField = field as TaxonomyField;
  if (taxonomyField != null
   && taxonomyField.SspId == termStore.Id
   && taxonomyField.TermSetId == termSet.Id)
  {
     // here taxonomyField is the wanted field
  }
}
share|improve this answer
    
Added to answer –  Per Jakobsen Mar 27 '12 at 6:37
    
Thank you for this. What I actually wanted to know is how to get an already exisiting taxonomy field from the title eg. something like var taxonomyField = web.Fields.GetField("TaxonomyFieldGroup", "TaxonomyFieldTitle") as TaxonomyField –  threela Mar 27 '12 at 7:28
    
Many thanx, thats exactly what I was looking for. Unfortunately I dont have enough reputations to vote for your answer :( –  threela Mar 27 '12 at 9:04
    
But you should be able to accept it –  Per Jakobsen Mar 27 '12 at 9:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.