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.
Select the Check Box.
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.
Happy Coding,
Sathish Nadarajan.
Leave a comment