简体   繁体   中英

Optimize CSOM loading time to get all files from SharePoint tree

so i'm trying to implement a function to get every files from a SharePoint tree. The function works fine except for the loading time of it.

Here is what i've done

    private List<ListItem> GetAllItems(ClientContext Context, List list, List<ListItem> ListItems, CamlQuery camlQuery)
        {
            camlQuery.ViewXml =
                    @"< View Scope = 'RecursiveAll'>
                        < Query >
                            <Where>
                          </Where>
                        </ Query >
                    </ View >";

            ListItemCollection AllItems = list.GetItems(camlQuery);
            Context.Load(AllItems);
            Context.ExecuteQuery();
            foreach (ListItem item in AllItems)
            {
                if (item.FileSystemObjectType == FileSystemObjectType.File)
                {
                    if (!ListItems.Contains(item))
                        ListItems.Add(item);
                }

                if (item.FileSystemObjectType == FileSystemObjectType.Folder)
                {
                    camlQuery.FolderServerRelativeUrl = item.FieldValues["FileRef"].ToString();
                    GetAllItems(Context, list, ListItems, camlQuery);
                }
            }

            return ListItems;
        }

The problem is Context.ExecuteQuery(); is taking me 0,3 - 1s to load each query and for a tree where i have 1424 folders it took me more than 4 min to load and it would be too long if the number of folders gets bigger.

So i want to ask is there any possibility to reduce loading time of this or is there anyway that is more optimal to get every files from a SharePoint tree?

This is an article about optimizing CSOM code for your reference: https://www.codeproject.com/Articles/738008/Programming-Efficiently-with-the-SharePoint-Client

Main idea:

  1. Only Request What You Want (But request everything you want in one go!)
  2. Call ExecuteQuery Sparingly
  3. Advanced: Parallel and Async Code
  4. Caching Data in the Session

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM