Moon
Moon

Reputation: 35265

Dynamically specifying lambda function parameters

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);
}

Upvotes: 0

Views: 539

Answers (3)

bmm6o
bmm6o

Reputation: 6505

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()
            );

Upvotes: 2

shabdar
shabdar

Reputation: 356

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.

Upvotes: 0

Heinrich
Heinrich

Reputation: 2214

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.

Upvotes: 0

Related Questions