tags:

views:

2400

answers:

2

Is there a way other than looping throug the Files in a SPFolder to determine if a give filename (string) exists?

+1  A: 

Using a CAML query is the most efficient way (example here)

CAML can be a bit unwieldy, so also worth looking at the Linq to Sharepoint provider, which hides the details of CAML away from you.

Paul Nearney
+5  A: 

You can, if you know the URL also use the SPFile.Exists property as follows:

using (SPSite site = new SPSite("http://server/site"))
using (SPWeb web = site.OpenWeb())
{
  SPFile file = web.GetFile("/site/doclib/folder/filename.ext");
  if (file.Exists)
  {
    ...
  }
}

One would on first thought assume SPWeb.GetFile throws an exception if the file does not exist. But as you see this is not the case - it will actually return a SPFile object.

Lars Fastrup