Wednesday, 8 May 2013

Check for existance of a file in Sharepoint

I was recently writing some simple code to extract a file from a document library. The code was simple enough, but it was the return value that got me into trouble.

The code is as follows:

  using (SPSite site = new SPSite(
    using (SPWeb web = site.RootWeb())
        SPFile file = web.GetFile("my file url");

All good so far. However, using my .Net brain is did a 'if (file != null)' .... oops.
The file object is never null.

The correct way to check the result if (if (file.Exists) .....).


No comments:

Post a comment