Question

Following this msdn page on SharePoint development I have encountered the following issue that I do not understand.

Case scenario: We are trying to retrieve List of all visible Lists that are present on a SharePoint site, together with their Title & ID.

Here is what Microsoft suggests:

    public static void RetrieveAllListOnWebpage()
    {
        using (ClientContext context = new ClientContext(Globals.Globals.SiteUrl))
        {
            var myList = context.Web.Lists;

            context.Load(myList,
                items => items.Where(item => item.Hidden == false)
                              .Include(item => item.Title, item=>item.Id));

            //context.Load(collListItem);
            context.ExecuteQuery();


            foreach (var item in myList)
            {
                Console.WriteLine($"ID: {item.Id}, Title: {item.Title}");
            }
        }
    }

When one tries to manipulate final Console.WriteLine statement to include more properties such as Created, it suddenly fails as we did not request this particular property in the ...Include(...item=>item.Created).

However when we change our initial context.Load() method to the following:

context.Load(myList,items => items.Where(item => item.Hidden == false));

it suddenly works without a problem no matter what properties we request.

The question now is - why does Microsoft suggest to use .Include() in our Context.Load() method?

One reason that came to my minds was that we are trying to minimize our footprint in the Sharepoint. This means that we in case we have a large number of Lists on our page (e.g. 100+) and we are only interested on two properties (Title & Created), and by limiting our search result we can get the response much faster which will not overload SharePoint too much with our request... Is this assumption correct?

Était-ce utile?

La solution

I believe you already know how CSOM works in SharePoint though I am adding a little explanation. CSOM sends XML request to Client.svc and get JSON response.

enter image description here

So when you write something like following, then it requests for all properties in XML request.

context.Load(myList, items => items.Where(item => item.Hidden == false)

Now consider following code

context.Load(myList,
                items => items.Where(item => item.Hidden == false)
                              .Include(item => item.Title, item=>item.Id));

It does not include all properties in the XML request. It only requests for Title and Id in the XML request.

So we can say that Include limits XML request for better performance. Anyway what you said in your last block is correct.

One reason that came to my minds was that we are trying to minimize our footprint in the Sharepoint. This means that we in case we have a large number of Lists on our page (e.g. 100+) and we are only interested on two properties (Title & Created), and by limiting our search result we can get the response much faster which will not overload SharePoint too much with our request... Is this assumption correct?

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top