Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations

Ghost Files

For some obscure reason, Windows Sharepoint Services allows me to fetch a non-existent file.

Consider this code:
SPSite site = new SPSite(site_url);
SPFile file = site.RootWeb.GetFile(file_url);

This code will return an SPFile object regardless of whether file_url points to an existing file or not.
To find out if the file exists, you need to call the file.Exists() method.
Even if we ignore the fact that it's against the common practice of throwing exceptions for these situations, it still behaves oddly.

You would expect that the properties for a non-existent file would be null, or at least throw exceptions - and indeed, most of them do throw (more on that later), but some of them DON'T - properties like the Name and URL of the file return values that are derived from the original URL I passed it, even though they do not, in fact, point to an existing file.

And the other properties DO throw, but they either throw a System.ArgumentOutOfRangeException, which makes absolutely no sense, or else a SPException specifying that the document library doesn't exist. The same property can throw a different one of the above exceptions, depending on whether the URL we gave it points to a non-existing file in an existing doc library, or to a doc library that doesn't exist at all.

It's a mess, I tell you - a mess.

Posted: Jun 10 2004, 01:31 AM by AvnerK | with 1 comment(s)
Filed under: ,

Comments

Greg said:

My undersatanding is that the resource kit released this week includes a tool to deal with ghost files. It's also been available from MSFT if I recall correctly, but you have had to go ask for it. Have not received my copy of the ResKit yet, so can't confirm it's there.
# June 10, 2004 10:24 AM