Programmatically enable Enterprise keywords in SharePoint 2010


Introduction: 

Enterprise keywords are used to tag the documents when uploading the documents in the document library. Enterprise documents can be enabled in both SharePoint lists and libraries. Enterprise keywords - Terms are stored in a non-hierarchical way and available for users to key in which is known as folksonomy (free tagging). In this article we will be seeing how to enable Enterprise keywords for a list using SharePoint object model.

Prerequisites:
  1. Managed metadata service application should be configured. 

  2. Web application should be associated to the Managed Metadata Service Application.
Programmatically Enable Enterprise Keywords:

To enable Enterprise keywords through UI refer http://www.c-sharpcorner.com/UploadFile/anavijai/7633.

To enable enterprise keywords using object model do the following steps:
  • Open Visual Studio 2010. 

  • Go to File => New => Project. 

  • Select Console Application template from the installed templates. 

  • Add the following references.

    Microsoft.SharePoint 

  • Add the following namespaces.

    using Microsoft.SharePoint;

  • Replace Program.cs with the following code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    namespace EnterpriseKeywords
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (SPSite site = new SPSite("http://serverName:22222/sites/Test/"))
                {
                    using (SPWeb web = site.OpenWeb("BL"))
                    {
                        SPList list = web.Lists.TryGetList("dl");
                        if (list != null)
                        {
                            SPField field = list.ParentWeb.AvailableFields["Enterprise Keywords"];
                            if (!list.Fields.ContainsField("Enterprise Keywords"))
                            {
                                list.Fields.Add(field);
                                list.Update();
                                Console.WriteLine(field.Title + " added successfully to the list");
                            }
                            else
                            {
                                Console.WriteLine(field.Title + " column already exists in the list");
                            }
                        }
                        else
                        {
                            Console.WriteLine(list.Title + " does not exists in the site");
                        }
                        Console.ReadLine();
                    }
                }
            }
        }
    }

  • Build the solution.

  • Hit F5.

  • Go to the =>Permissions and Management => Enterprise Metadata and Keywords Settings.

  • You could see the "Enterprise Keywords" enabled.

    Enterprisekeywords1.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all