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


Sathish Nadarajan
SharePoint MVP
Published On :   05 Jan 2017
Visit Count
Today :  1    Total :   2275
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


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.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

SharePoint Analytics