0

There are hundreds of fields in the library but I am interested in just a few. The following code does not work:

string[] fieldNames = {"field1", "field2", ..., "fieldN"};
var fields = context.LoadQuery(context.Web.Lists.GetByTitle(" My List").Fields
   .Where(x => fieldNames.Contains(x.Title))
   .Include(fld =>fld.Title, fld=>fld.InternalName));

The run-time message is "The 'Contains' member cannot be used in the expression.".

I could write .Where(x => (x.Title == "field1" || x.Title == "field2" ...)) or construct a CAML query dynamically, but is there a shorter way?

| improve this question | | | | |
1

As far as I know and as far as I can find on the Internet, the Any and Contains keywords are not supported in this scenario.

So you are stuck with using a fixed number of conditions with the || operator or a dynamically constructed CAML query, unless someone else may be able to provide a different solution.

| improve this answer | | | | |
  • This code produces the same result: "The 'Any' member cannot be used in the expression". – Test User May 28 '17 at 21:22
  • Even if it ran, would it do what I want it to do: From MSDN: "This method does not return any one element of a collection. Instead, it determines whether any elements of a collection satisfy a condition." The set of all fields in the list must include the small subset that I want. – Test User May 28 '17 at 21:32
  • You are right. I should have tested the Any and Contains keywords before posting an answer. I am going to update my answer with a working example. – Mihail-Florin Popa May 29 '17 at 10:14
  • Loading in a loop (see answer below) seems as if it will work. If I wanted to try a CAML query, which list would I query? I need the structure of the list, not the contents. Does SharePoint expose lists like the INFORMATION_SCHEMA views in MS SQL Server? – Test User May 29 '17 at 17:42
0

I found an article which uses foreach loop to automate the include fields. This might solve your problem.

var fieldNames = new[] { "Title", "Model" };
foreach (var fieldName in fieldNames)
{
    context.Load(items, includes => includes.Include(i => i[fieldName]));
}
context.ExecuteQuery();

Syntax for including fields dynamically in CSOM query?

| improve this answer | | | | |
  • This looks good. I will be able to test it in a few hours. – Test User May 29 '17 at 17:42
  • The line context.Load(myList.Fields, includes => includes.Include(i => i[fieldName])); is syntactically incorrect. The message is "Cannot apply indexing with [] to an expresion of type'Field'". When I change it to context.Load(myList.Fields, includes => includes.Include(i => i.Title == fieldName)); the message is 'The query expression is not supported'. – Test User May 29 '17 at 21:26
  • The line context.Load(items, x => x.Where(i => i.Title == fieldName)); is OK on the first iteration but on the next it throws an exception "The query expression is not supported". – Test User May 29 '17 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.