0

Using SharePoint Server object model how can you retrieve a specific document by name in a Document Library, so that the file can be downloaded programatically?

I am finding a lot of articles with examples of iterating through a list or document library. I would like to be able to retrieve a specific file by name, so that I can get the bytes or stream. Is this possible?

Thanks for any tips or advise.

1 Answer 1

0

You can use SPWeb.GetFile() method to retreive the document by its name.

Here's the example part of the code which can be used to solve your problem:

string s = "http://sp/Library/filename.png";
var f = web.GetFile(s);
if (f.Exists)
{
    var b = f.OpenBinary();
    System.IO.File.WriteAllBytes("C:\\temp\\fileName.png", b);
}

The web variable is the SPWeb that contains your library. You should create that object yourself or use the one from SPContext. You should also change the url of the file to match your file (variable s in my example).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.