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

3 Comments

  1. wwei says:

    Using ‘list.Fields.ContainsField’ works better for me.

    if (list.Fields.ContainsField(col.ColumnName))
    {
    //
    }

  2. will says:

    Thanks for the tip wwei. That helped.

  3. Peyton Stennett says:

    I think this is a real great post. Want more.

Leave a Reply

Your email address will not be published. Required fields are marked *