Reputation: 11327
The SPSite constructor (new SPSite(mySiteId)
) returns a cached entry, even though the site no longer exists. How can I prevent this?
Upvotes: 0
Views: 301
Reputation: 1289
A little late maybee, but since I had this same problem.
You can call the satic method InvalidateCacheEntry(Uri uri, Guid siteId) on the SPSite class. By passing an empty guid and the uri of the SPSite you are using, you should be able to clear the cache and get the current values.
Notice that this seems to be also the cause of the issue of SPSite.Exists returning "true" for just deleted sites. By using the InvalidateCacheEntry method, I was able to detect the correct state of the site (deleted or existent). I don't know what is the performance cost of this workaround, but please consider it if you have a similar problem in the future.
Upvotes: 0
Reputation: 27455
We had the same problem and haven't found a solution for it yet. We tried to check whether a SPSite exists or not by calling the static SPSite.Exists(..) method. The method returned true also for sites that didn't exist any more.
But we have found a little workaround. We try to provoke a FileNotFoundException by calling the SPSite's Usage property. When the exception arise we know that the site doesn't exist any more.
After catching the exception you can call again the SPSite.Exists() method which will now return false.
Upvotes: 1
Reputation: 117230
While I do not know the Sharepoint details, I can tell you calling new
will NEVER return a cached object. It will ALWAYS be a newly allocated object, although the internals may point to cached objects.
Upvotes: 0