In a Sharepoint there is a SPList that I should check if a name of field exist. (If exist I add content, if not exist I do something else)
Now I’m doing that:
SPListItemCollection listItems = spList.GetItems();
SPFieldCollection spFieldCollection =listItems.Fields;
foreach (SPField field in spFieldCollection)
{
String name = field.Title;
if (name == "nameField") {
return true; // Exist
}
}
that works ok, except if the list is empty. How can I check if a name of the field exist before add content to the list?
Just check on the
Fieldsproperty on theSPList:Use the method
ContainsFieldto check if a field exists:SPFieldCollection.ContainsField Method