How to Enable Enterprise Metadata and Keywords Settings in SharePoint Office 365 Lists Programmatically using CSOM C#

Sathish Nadarajan
 
Solution Architect
January 5, 2017
 
Rate this article
 
Views
5786

In this article, let us see how to enable Enterprise Metadata and Keywords Settings in SharePoint Office 365 Lists Programmatically using CSOM C#

To do that Manually, we need to go to the List settings, click on the below highlighted link as shown in the figure.

 

image

 

Select the Check Box.

 

image

 

The New column called “Enterprise Keywords” will be added to the list.

To do this through program, we need to do the reverse way. If we Add the column “Enterprise Keywords”, then the “Enterprise Keywords” settings will be enabled automatically.

Hence, the code to add the field to the list is as follows.

 namespace Sathish.Demo
 {
     using Microsoft.SharePoint.Client;
     using System;
 
     class Program
     {
         static void Main(string[] args)
         {
             EnableEnterpriseKeywords();
         }
 
         public static void EnableEnterpriseKeywords()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
             
             string siteUrl = "https://*******.sharepoint.com/sites/CommunitySite";
             string userName = "Sathish@******";
             string password = "******************";
 
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 try
                 {
                     Web web = clientContext.Web;
                     
                     clientContext.Load(web);
                     clientContext.Load(web.Lists);
                     clientContext.ExecuteQuery();
 
                     List list = web.Lists.GetByTitle("Documents");
 
                     if (!list.FieldExistsByName("Enterprise Keywords"))
                     {
                         var field = clientContext.Site.RootWeb.Fields.GetByInternalNameOrTitle("Enterprise Keywords");
                         list.Fields.Add(field);
                         clientContext.Load(list);
                     }
 
                      
 
                     clientContext.ExecuteQuery();
 
                 }
                 catch (Exception ex)
                 {
                     System.Console.ForegroundColor = ConsoleColor.Red;
                     System.Console.WriteLine("Exception Occured : " + ex.Message);
                     System.IO.File.AppendAllText("C:\Temp\Exception.txt", ex.Message + " - " + siteUrl + Environment.NewLine);
                 }
             }
 
             System.Console.WriteLine("Completed....");
             System.Console.WriteLine("Press Any Key to Exit ....");
             System.Console.ReadLine();
         }
     }
 }
 

The output will be like this.

 

image

 

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment