Check if a list column exists using the SharePoint client object model?

Using the client object model (C #) in SharePoint 2010, how to determine if the specified column (field) name exists in this list?

Thanks, MagicAndi.

+7
client-side sharepoint sharepoint-2010 csom
source share
8 answers

I just found this while searching for the same, but Sharepoint 2010 seems to have something for this, at least for the server model: list.Fields.ContainsField("fieldName");

Not sure if it exists for the client side. It is clear that this will be a good place to store this information.

+11
source share

Server object model

 string siteUrl = "http://mysite"; using (SPSite site = new SPSite(siteUrl)) { using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists["my forum"]; for (int i = 0; i < list.Fields.Count; i++) { if (list.Fields[i].Title == "xyz") { - - } } } } 

Client Object Model

 string siteUrl = "http://MyServer/sites/MySiteCollection"; ClientContext clientContext = new ClientContext(siteUrl); SP.List List = clientContext.Web.Lists.GetByTitle("my forum"); for (int i = 0; i < list.Fields.Count; i++) { if (list.Fields[i].Title == "xyz") { - - } } 
+8
source share

The following method demonstrates how to determine if a specified column exists in a List using CSOM :

 static class FieldCollectionExtensions { public static bool ContainsField(this List list,string fieldName) { var ctx = list.Context; var result = ctx.LoadQuery(list.Fields.Where(f => f.InternalName == fieldName)); ctx.ExecuteQuery(); return result.Any(); } } 

Using

 using(var ctx = new ClientContext(webUrl)) { var list = ctx.Web.Lists.GetByTitle(listTitle); if(list.ContainsField("Title")){ //... } } 
+3
source share

Here's the extension code (CSOM) for the sharepoint list

  public static bool DoesFieldExist(this List list, ClientContext clientContext, string internalFieldname) { bool exists = false; clientContext.Load(list.Fields, fCol => fCol.Include( f => f.InternalName ).Where(field => field.InternalName == internalFieldname)); clientContext.ExecuteQuery(); if (list.Fields != null && list.Fields.Count > 0) { exists = true; } return exists; } 

Using

 List targetList = this.Context.Web.Lists.GetById(<ListID>); targetList.DoesFieldExist(<ClientContext>, <Field internal Name>) 

enjoy :)

+3
source share

I finished searching for the details of the list fields before my operation and saved them in the general list of structures (containing information about each field). I then query this (generic) list to see if the current field really exists in this (SharePoint) list.

 // Retrieve detail sof all fields in specified list using (ClientContext clientContext = new ClientContext(SharePointSiteUrl)) { List list = clientContext.Web.Lists.GetByTitle(listName); _listFieldDetails = new List<SPFieldDetails>(); // get fields name and their types ClientObjectPrototype allFields = list.Fields.RetrieveItems(); allFields.Retrieve( FieldPropertyNames.Title, FieldPropertyNames.InternalName, FieldPropertyNames.FieldTypeKind, FieldPropertyNames.Id, FieldPropertyNames.ReadOnlyField); clientContext.ExecuteQuery(); foreach (Field field in list.Fields) { SPFieldDetails fieldDetails = new SPFieldDetails(); fieldDetails.Title = field.Title; fieldDetails.InternalName = field.InternalName; fieldDetails.Type = field.FieldTypeKind; fieldDetails.ID = field.Id; fieldDetails.ReadOnly = field.ReadOnlyField; listFieldDetails.Add(fieldDetails); } } // Check if field name exists _listFieldDetails.Exists(field => field.Title == fieldName); // Struct to hold details of the field public struct SPFieldDetails { public string Title { get; set; } public string InternalName { get; set; } public Guid ID { get; set; } public FieldType Type { get; set; } public bool ReadOnly { get; set; } } 
+2
source share

Some good answers are above. I personally used this:

  List list = ctx.Web.Lists.GetByTitle("Some list"); FieldCollection fields = list.Fields; IEnumerable<Field> fieldsColl = ctx.LoadQuery(fields.Include(f => f.InternalName)); ctx.ExecuteQuery(); bool fieldMissing = fieldsColl.Any(f => f.InternalName != "Internal_Name"); 

You can also use the Where method after Include and check if the returned value of the / field collection is returned. This applies to personal preferences, since both options are requested on the client side.

+2
source share

I prefer the SharePoint Plus library because it is really clean: http://aymkdn.imtqy.com/SharepointPlus/symbols/%24SP%28%29.list.html

 $SP().list("My List").get({ fields:"Title", where:"Author = '[Me]'" },function getData(row) { console.log(row[0].getAttribute("Title")); }); 

You can set up a for loop for a loop in a row and check if the column you are looking for exists.

0
source share

for big code use this

Loading fields first and then

  bool exists= clientContext2.Site.RootWeb.Fields.Any(o => o.Id.ToString() == a.Id.ToString()); 
-2
source share

All Articles