SPSite.Exists () returns true, although the site collection does not exist

I am currently working on TimerJob, which manages some site collections. When the work is done, it scans the list to get the URL of the site SPSite.Exists() , then it calls SPSite.Exists() to check if the site still exists or not.

To check TimerJob, I delete the site collection, but left the corresponding entry in the list. Then I run TimerJob and execute its code in debug mode. When it comes to the point, to check if the SPSite.Exists() site SPSite.Exists() , returns true.

When I run TimerJob a second time for the same site SPSite.Exists() , the SPSite.Exists() method returns false, as it should.

So now I am wondering why SPSite.Exists() returns a false result when I run the task for the first time. Could this be caused by caching?

When I run the same code outside of TimerJob SPSite.Exists() , the correct result is returned each time.


UPDATE

So, I did some more debugging, and it seems that this problem was really caused by some kind of caching mechanism, since this does not happen when the Windows SharePoint Services Timer service was restarted after deleting the test site collection and before TimerJob was started.

At the moment, I cannot imagine any other solution than trying to access the remote site and catch the exception that will be thrown to determine if the site really exists.


UPDATE 2

After several tests, I realized that the problem does not occur for the first call to SPSite.Exists () (in TimerJob) after restarting the timer service. The second call (for another site collection) still leads to a known problem.


UPDATE 3

I am currently using ugly hacking to solve my problem. When SPSite.Exists () returns true, although it doesn’t actually exist, I create an SPSite object and try to throw a FileNotFoundException by calling its Usage property. When exceptions arise, I know that the site does not exist. Oddly enough, after the exception, SPSite.Exists () was reset and returns the correct result (false).

Are there any other suggestions?

Bye flo

+6
sharepoint timer-jobs
source share
2 answers

The same goes for me. I had a similar problem after I deleted the collection site. I am still true for SPSite.Exists(); The strange thing was that if I opened the remote URL for the collection of sites in the browser, the first request would result in an HTTP 400 error message, while the second request was the expected HTTP 404.

My workaround was to simply create an HTTP GET for the URL to create the first request, and then check for the site again.

  private void touchWeb(string url, System.Net.ICredentials credentials) { try { Uri uri = new Uri(url); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Credentials = credentials; request.Method = "GET"; string result = ""; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8)) { result = readStream.ReadToEnd(); } } } } catch (Exception) { } } 
+4
source share

I had the same problem and tried the HTTP request method, but found that it was somewhat slow to check for a large number of sites at the same time. Instead, I used something like this:

 public bool SPSiteExists(string url) { SPSite.InvalidateCacheEntry(new Uri(url), Guid.Empty); return SPSite.Exists(uri); } 
+5
source share

All Articles