Determine If SharePoint List Column Exists In C#

This method is helpful if you need to determine whether a column in a list exists or not by passing in the column name as a string value.

It is a helpful method when working with SharePoint field existence.

[csharp]

public bool determineIfColumnExists(ref string nonExistingColumnName, params string[] columnNames)
{
nonExistingColumnName = string.Empty;
bool flagExists = false;
if (this.List != null)
{
foreach (string columnText in columnNames)
{
if (columnText != string.Empty)
{
try
{
SPField columnField = this.List.Fields.GetFieldByInternalName(columnText);
flagExists = columnField != null;
}
catch
{
flagExists = false;
}
if (!flagExists)
{
nonExistingColumnName = columnText;
return flagExists;
}
}
}
}
return flagExists;
}
[/csharp]

Share