SPSite constructor returns cached instance.

299 views Asked by At

The SPSite constructor (new SPSite(mySiteId)) returns a cached entry, even though the site no longer exists. How can I prevent this?

3

There are 3 answers

0
Flo On

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.

3
leppie On

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.

0
SPArcheon On

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.