How to Check if Column Exists or Not in SharePoint List using CSOM

In this SharePoint tutorial, we will discuss, how to Check if Column Exists or Not in SharePoint List using CSOM using the C#.Net client object model.

We will use to develop the demo in a windows application where we have added the two required client-side dll:

  • Micrsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll

We will connect to a SharePoint online site as well as a SharePoint 2016 site.

Check if Column Exists or Not in SharePoint List using CSOM

Here I have a list name as “MyDemoList” in my SharePoint 2016on-premisee site. In that list we will check if a column name as “MyTestColumn” exists or not using SharePoint 2016 client object model.

The same code will work in SharePoint 2013. Here in the windows application, we have added a button and the code we wrote on the button click.

Below is the full code:

private void btnOnPremise_Click(object sender, EventArgs e)
{
using (ClientContext context = new ClientContext("http://mypc/sites/MySP2016SiteCollection/"))
{
var list = context.Web.Lists.GetByTitle("MyDemoList");
context.Load(list);
context.Load(list.Fields);
context.ExecuteQuery();
for (int i = 0; i < list.Fields.Count; i++)
{
if (list.Fields[i].Title == "MyTestColumn")
{
label1.Text = "Column Exists";
return;
}
}
}
}

Once you run the code, you can see it will display the column exists like below:

SharePoint 2016 csom check if column exists in list
SharePoint 2016 csom check if column exists in list

Check If Column exists in SharePoint Online List using (C#.net client object model) CSOM

The same way we can also check if list column exists or not in SharePoint online using csom. Here we have the same list name as “MyDemoList” presented in SharePoint online. Here also we will check if column name as “MyTestColumn” is exists or not in the list.

Below is the full code which we wrote on a button click in the windows application.

private void btnOnline_Click(object sender, EventArgs e)
{
using (ClientContext clientContext = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
clientContext.Credentials = new SharePointOnlineCredentials("bijay@onlysharepoint2013.onmicrosoft.com", GetSPOSecureStringPassword());
List list = clientContext.Web.Lists.GetByTitle("MyDemoList");
clientContext.Load(list);
clientContext.Load(list.Fields);
clientContext.ExecuteQuery();
for (int i = 0; i < list.Fields.Count; i++)
{
if (list.Fields[i].Title == "MyTestColumn")
{
label1.Text = "Column Exists";
return;
}
}
}
}
private SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in "*********")
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}

Once you run the above code, you can see the column exists like below:

SharePoint online csom check if column exists in list
SharePoint online csom check if column exists in list

Read some SharePoint CSOM Tutorials:

I hope this tutorial helps to learn how to Check if Column Exists or Not in SharePoint List using CSOM.

>