Ghost Files

Tags: .NET, SharePoint

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.

1 Comment

  • 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.

Comments have been disabled for this content.