2

I'm using the SharePoint Client Object Model to query lists in Sharepoint and need to create the Linq queries dynamically in a helper assembly.

How do I create the following from a list<string> of column names containing "Id","Title","LinkFileName","Status" ?

context.Load(listItems, items => items.Include(  
  item => item["Id"],  
  item => item["Title"],  
  item => item["LinkFilename"],  
  item => item["Status"]  
));     

1 Answer 1

4

a BIG thank you to Manvir for his post that solved my problem:

http://www.manvir.net/invalidqueryexpressionexception-the-query-expression-is-not-supported/

Replace the ClientContext.Load with ClientContext.LoadQuery that does support runtime-built expressions.

private static Expression<Func<ListItem, object>>[] CreateListItemLoadExpressions(List<string> viewFields)
{
   List<Expression<Func<ListItem, object>>> expressions = new List<Expression<Func<ListItem, object>>>();
   foreach (string viewFieldEntry in viewFields)
   {
      string fieldInternalName = viewFieldEntry;
      Expression<Func<ListItem, object>> retrieveFieldDataExpression = listItem => listItem[fieldInternalName];
      expressions.Add(retrieveFieldDataExpression);
   }
   return expressions.ToArray();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.