Test Field Exists in SharePoint 2010 Client Object Model
When testing if the field exists using Client Object Model, it is better to return the Field object at same time and test if the Field object is null. If it is not null, you can get the "Field" object and do more processing.
public static Field GetFieldByName(ClientContext context, Guid webId, Guid listId, string fieldName)
{
{
Web web = context.Site.OpenWebById(webId);
List list = web.Lists.GetById(listId);
// Test if the field exists
var q = from f in list.Fields
where f.Title == fieldName
select f;
var fields = context.LoadQuery(q);
context.ExecuteQuery();
if (fields.Count() == 0)
return null;
// Get the field and return it
Field field = list.Fields.GetByInternalNameOrTitle(fieldName);
context.Load(field, f => f.Id, f => f.Title, f => f.InternalName);
context.ExecuteQuery();
return field;
}
This comment has been removed by a blog administrator.
ReplyDelete