1

I have a list on SharePoint, where I am trying to load fields for this list like following:

        var lists = context.Web.Lists;
        context.Load(lists, n => n.Include(x => x.Title,
                                           x => x.Fields.Include(
                                               z => z.Title, 
                                               z => z.InternalName, 
                                               z => z.TypeDisplayName)));
        context.ExecuteQuery();

The following part of the code I use quite often in my code, while working with other lists:

                          x => x.Fields.Include(
                                               z => z.Title, 
                                               z => z.InternalName, 
                                               z => z.TypeDisplayName)

I would like to know if there is a way how to simplify inserting this .Include statement in my code (so that in case I will have to add more properties in the future, I dont have to rewrite all my code everywhere but only in one place).

I have tried to create custom LINQ Extension, but it failed as it is probably expecting sort of a Expression<Func<T,Y>> (I guess).

Any help regarding this matter would be more than appreciated!

0

This is the result I was looking for:

 private static readonly Expression<Func<Field, Object>>[] LoadFieldProperties = { term => term.InternalName,
                                                                                   term => term.Title,
                                                                                   term => term.TypeDisplayName};

and then call it like following:

var lists = context.Web.Lists;
context.Load(lists, n => n.Include(x => x.Title,
                                        x => x.Fields.Include(LoadFieldProperties)));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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