4

How can I dinamically 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: http://blogs.technet.com/b/speschka/archive/2009/11/01/using-the-sharepoint-2010-client-object-model-part-3.aspx

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."

dsanatelli
  • 66
  • 1
  • 3

3 Answers3

0

You could create a specific view with the columns you want to query later on and use that view when calling the GetItems method.

lem.mallari
  • 1,274
  • 12
  • 25
0

I would recommend you the free CAMLDesigner (http://sharepoint.biwug.be/SitePages/Caml_Designer.aspx) - there you can create your caml and check the results in a nice GUI.

https://sharepoint.stackexchange.com/a/69172/5170

Community
  • 1
  • 1
saber tabatabaee yazdi
  • 4,404
  • 3
  • 42
  • 58
0

If you want to dynamically specify which fields to load via CamlQuery you can load each field in loop:

var array = new string[] { "ID", "Title" }; // define dynamically, this is just an example
foreach (var field in array)
{
    clientContext.Load(listItems, includes => includes.Include(i => i[field]));
}

The generated query is exacly the same as multiple lambda expressions in one Load method.

clientContext.Load(listItems, includes => includes.Include(i => i["ID"], i => i["Title"]));

Both generates query:

<Query Id="#" ObjectPathId="#">
    <Query SelectAllProperties="false">
        <Properties />
    </Query>
    <ChildItemQuery SelectAllProperties="false">
        <Properties>
            <Property Name="ID" ScalarProperty="true" />
            <Property Name="Title" ScalarProperty="true" />
        </Properties>
    </ChildItemQuery>
</Query>
tinamou
  • 2,282
  • 3
  • 24
  • 28