Programmatically set value to the Taxonomy Field in SharePoint 2010

In this article we will be seeing how to set value to the Taxonomy Field in SharePoint 2010 using SharePoint Object Model.


Introduction:

I have created a custom list which contains the following columns

Taxonomy Field in SharePoint 2010

I have the following Group, Term Set and Terms in the term store

Taxonomy Field in SharePoint 2010

Term Set Settings for TaxonomyField:


Taxonomy Field in SharePoint 2010

Steps Involved:

  1. Open Visual Studio 2010.
  2. On the File Menu, click on New and then click on Project.
  3. Select Console Application template from Installed templates.
  4. Check whether the project is targeted to .Net Framework 3.5.
  5. Enter the Name for the project and then click on Ok.
  6. Right click on the project and then click on Properties.
  7. Click on Build tab, and check whether the Platform Target is selected as Any CPU.
  8. Add the following references.

    • Microsoft.SharePoint.dll
    • Microsoft.SharePoint.Taxonomy.dll
     
  9. Add the following namespaces.

    • Using Microsoft.SharePoint;
    • Using Microsoft.SharePoint.Taxonomy;
     
  10. Replace Program.cs with the following code.

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

    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (SPSite site = new SPSite("http://serverName:25374/sites/TeamEng02/"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        TaxonomySession taxonomySession=new TaxonomySession (site);
                        TermStore termStore=taxonomySession.TermStores["Metadata Service Application Proxy"];
                        Group group=termStore.Groups["Group"];
                        TermSet termSet=group.TermSets["TermSet"];     
                        Term term=termSet.Terms["Term"];
                        SPList list = web.Lists.TryGetList("Test");
                        if (list !=null)
                        {
                            TaxonomyField taxonomyField = list.Fields["TaxonomyField"]as TaxonomyField;
                            TaxonomyFieldValue taxonomyFieldValue = new TaxonomyFieldValue(taxonomyField);
                            taxonomyFieldValue.TermGuid = term.Id.ToString();
                            taxonomyFieldValue.Label = term.Name;
                            SPListItem item = list.Items.Add();
                            item["Title"] ="Sample";
                            item["TaxonomyField"] = taxonomyFieldValue;
                            item.Update();
                            list.Update();
                        }
                        else
                        {
                            Console.WriteLine(list.Title + " does not exists in the list");
                        }
                        Console.ReadLine();                   
                    }
                }
            }
        }

     

  11. Build the solution.
  12. Hit F5.
  13. Go to the custom list, a new item will be created successfully.

    Taxonomy Field in SharePoint 2010

Conclusion:

Thus value is successfully added to the taxonomy field using SharePoint Object Model.