0

Suppose I have the following line of code:

context.Load(itemCollection, item => item.Include(i => i["Title"], i => i["Name"]));

Is there any way I can dynamically specify the parameters to the item.Include() function instead of hard-coding them as above?

I would ideally like to allow users to select properties they want to retrieve of an object such as Title, Name, Description, etc.

FYI, here is the ClientContext.Load function. This function is coming from Microsoft.SharePoint.Client.dll

public void Load<T>(T clientObject, params Expression<Func<T, object>>[] retrievals) where T : ClientObject
{
  if ((object) clientObject == null)
    throw new ArgumentNullException("clientObject");
  ClientAction.CheckActionParameterInContext(this, (object) clientObject);
  DataRetrieval.Load<T>(clientObject, retrievals);
}
Moon
  • 33,439
  • 20
  • 81
  • 132
  • yes, I make functions like this for MS CRM libraries all the time. You want to investigate `public void MyMethod(Func> expression) where t : ` extending the type you want in the expression. Anyway, search on Func for more examples – Mike_Matthews_II Mar 13 '13 at 00:53

3 Answers3

2

I don't have the necessary setup to test it, but will something like this work?

String[] keys = ...;
context.Load(   itemCollection
              , item => item
                        .Include(keys
                        .Select(key => { i => i[key] })
                        .ToArray()
            );
bmm6o
  • 6,187
  • 3
  • 28
  • 55
0

Have a look at the Answer for this question.
I had a similar problem with creating dynamic where clauses, and this is how I got around it.

Community
  • 1
  • 1
Heinrich
  • 2,144
  • 3
  • 23
  • 39
0

Use the following syntaxt (works in JavaScript, but haven't tested it in C#):

context.load(this.list, 'Include(Title, Name)');

'Include(Title, Name)' is a string and is easier to generate based on user input.

this.list refers to a sharepoint list that is loaded before.

shabdar
  • 371
  • 1
  • 8