Is there a way other than looping through the Files in a SPFolder to determine if a give filename (string) exists?
When you check out a document, users will still be able to access the latest version of the document in SharePoint document library, but only in Read mode. They will not be able to make any edits or save the document back to the library until the original document is checked back in.
SharePoint files can be access via a web URL. So to find that URL you can open the browser, then type the SharePoint site url. Then choose the library/folder you uploaded the file. Then you can right click the file and copy the URL.
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.
But if you are using SP 2010 Client OM, it would actually throw an exception if the file doesn't exist:
using(var clientContext = new ClientContext(site))
{
Web web = clientContext.Web;
Microsoft.SharePoint.Client.File file = web.GetFileByServerRelativeUrl("/site/doclib/folder/filename.ext");
bool bExists = false;
try
{
clientContext.Load(file);
clientContext.ExecuteQuery(); //Raises exception if the file doesn't exist
bExists = file.Exists; //may not be needed - here for good measure
}
catch{ }
if (bExists )
{
.
.
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With