The previous article briefly introduced the use of csom for programming. Today, I will talk about some tips in csom to make your program run faster.
Load some attributes separately
In the preceding example, we use the following code to return web object information:
VaR web = clientcontext. Web; clientcontext. Load (Web); // set Query Information
When querying Web information, the Web will return many attributes, many of which are not required. If we only need to return some attributes, we can use the following method:
var currentWeb = clientContext.Web;clientContext.Load(currentWeb, web => web.Title, web => web.Url, web => web.WebTemplate);clientContext.ExecuteQuery();
The returned message is in the following format:
We can see that only the required attributes are returned in the message format.
In the above two cases, the returned respone lengths are 1200 and 500, respectively. Therefore, if the network throughput is limited, the query attributes should be returned as little as possible, it plays an important role in improving efficiency.
Query set attributes
How does csom process the query of set information? For example, to obtain all list information on the Web, we can:
clientContext.Load(currentWeb.Lists);clientContext.ExecuteQuery();
At this time, all information about each list will be returned. If we only want to go back to the title and basetemplate of a list, we only need:
var currentWeb = clientContext.Web;clientContext.Load(currentWeb.Lists, lists => lists.Include(list => list.Title, list => list.BaseTemplate));
Or you can use:
clientContext.LoadQuery(currentWeb.Lists.Include(list => list.Title, list => list.BaseTemplate));clientContext.ExecuteQuery();
In both cases, in a web with 25 lists, the response packet lengths are 38963 and 8172, respectively. Therefore, when loading a set, selecting appropriate query conditions is crucial to performance.
Use SharePoint csom to write efficient programs