A hack for the Sharepoint list cache issue
So I noted that Sharepoint has an issue where querying a list with the same criteria will return cached results regardless of the access method, and so I found a hack for it. The idea for the hack goes back to an issue where I would get cached results from a Domino page that was supposed to have dynamic values.
With the Domino cache issue, the idea was to append an extra key/value pair onto the Domino URL that contained some sort of unique value, like the current time in milliseconds. That way, the Domino server would see the request as unique and not serve the cached version.
With Sharepoint, the issue is when you use the same CAML query on a list within a certain time frame. If you used the same CAML string, you would receive the same result set from the list even if you had made changes to the list between the two queries. So the strategy is to create a CAML query that is unique every time but still gets the results you need.
The result is to add a “Not Equal” node to your CAML query, that you know will never be true, but that contains a value that changes constantly.
Here is a query that if you execute it against a list twice, with changes between queries, will return the same results.
theQuery.Query="
<Where>
<And>
<Eq>
<FieldRef Name=""AssignedToUser"" />
<Value Type=""User"">" + theWeb.CurrentUser.ToString + "</Value>
</Eq>
<Eq>
<FieldRef Name=""Status"" />
<Value Type=""Choice"">Assigned</Value>
</Eq>
</And>
</Where>"
But by creating a random number, and comparing it to a field that you know will never be equal to that random value, you end up with a unique query for every request to the list.
Dim generator As New Random
Dim randomValue As Integer
randomValue = generator.Next(100, 1000000000)
theQuery.Query = "
<Where>
<And>
<And>
<Eq>
<FieldRef Name=""AssignedToUser"" />
<Value Type=""User"">" + theWeb.CurrentUser.ToString + "</Value>
</Eq>
<Eq>
<FieldRef Name=""Status"" />
<Value Type=""Choice"">Assigned</Value>
</Eq>
</And>
<Neq>
<FieldRef Name=""Title"" />
<Value Type=""Text"">" + randomValue.ToString + "</Value>
</Neq>
</And>
</Where>"
The <Neq> tag is the “Not Equal To” tag that introduces the random number into the query but does not effect the outcome since it is always true.
And THAT is a hack if I ever saw one!
_
February 12th, 2009 at 9:21 am
Thanks. This problem baffled me for a bit and I tried googling ways of invalidating the cache. This hack works well enough for me
December 3rd, 2009 at 11:32 am
Great read! thx
December 9th, 2009 at 8:41 am
Your posts are the best! Always helpful!
March 31st, 2010 at 11:45 am
Hi, I can’t comprehend how to add your site in my rss reader. Can you Assist me, please