SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I dynamically generate/specify a list of item fields I want to load when querying a list using the Client OM?

This is possible to do using the tag on the CAML query, but this loads additional unwanted fields, making the payload bigger. See here: blog

Here's the testing code im using:

ClientContext clientContext = new ClientContext("http://myserver/sites/mysite");
Web site = clientContext.Web;

List list = clientContext.Web.Lists.GetByTitle("MyList");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View Scope='RecursiveAll'><RowLimit>100</RowLimit></View>";
ListItemCollection listItems = list.GetItems(camlQuery);

clientContext.Load(listItems,
      items => items.ListItemCollectionPosition,
      items => items.Include(
              item => item["ID"],
              item => item["Title"]
              ));

clientContext.ExecuteQuery();

What I want to do is to generate the lambda expression's for the Include method at runtime. Still no luck. Every solution I try gives me error

"The query expression is not supported."

share|improve this question
    
Could you create a lambda expression for include dynamically? I need the same. Thanks, Manvir – user14132 Jan 15 '13 at 5:55

I had the same problem. The solution I found was to import the Microsoft.SharePoint.Client namespace.

using System;
using System.Linq;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

If the using for the Microsoft.SharePoint.Client namespace without the alias not is in the list you get the error as you mentiont in your question.

share|improve this answer

I found its resolution. Use LoadQuery instead of Load. Pls see complete solution here http://www.manvir.net/invalidqueryexpressionexception-the-query-expression-is-not-supported/

share|improve this answer
    
Its is best practice here to put some piece of code or information along with the reference/source link, and not just providing urls, some ppl might have other sites blocked, so its better they can read the stuffs here itself – Gaurravs Jan 21 at 4:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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