Monday, February 11, 2019

CrossListQueryInfo - questions of getting cached objects

According msdn CrossListQueryInfo class  is used to getting the cached results or, if there are no cached results available, to cross-list quering to the database and then caching the results for future use. But in practice, there are some questions of using it. Here they are.

1. Does it work without activating features «SharePoint Server Publishing Infrastructure» and «SharePoint Server Publishing»?
- Yes. You can change «Object cache settings» by the direct Url: <sitecollection> /_layouts/15/objectcachesettings.aspx


2. Is setting the option «Check the server for changes every time a cross list query runs» disable caching?
- No. It just checking of any changes in all cached lists before executing query. If there are any changes in any of lists – ALL data getting again from database at first queries(if you using class CrossListQueryInfo to retrieve data from multiple different lists). By the way, if «Use the cached result of a cross list query for this many seconds» option is set it will get data from database for getting information about actual cache before executing query too. The time of executing that queries to database almost the same in both cases.


3. Why my SharePoint Cross-list Query Not Using Cache?
- There may be some reasons. First is described here. You need to set WebUrl property to ServerRelativeUrl (not absolute url). Second and third are described here. You have to call GetSiteData correctly : GetSiteData(SPSite site) not GetSiteData(SPWeb web) and set UseCache property to true.


4. Why in some cases if I did everything right, and set UseCache to true, do I get an «Object reference not set to an instance of an object.» error?
- Probably, you set Query property of CrossListQueryInfo to null. Try set it to String.Empty.