How To Download A File From A Document Library Using The SharePoint Client Object Model



TODO:

Have you ever wanted to download a document from a document library using the SharePoint client object model?

 

SOLUTION:

using SP = Microsoft.SharePoint.Client;

//class and code go here

string saveLocation = "C:\\Temp\\";

//SET YOUR ITEM HERE!!!!!!
SP.ListItem item = null;

using (SP.FileInformation fileInformation = SP.File.OpenBinaryDirect(put your context here, (string)item["FileRef"]))
{
     string fileName = (string)item["FileRef"];
     fileName = string.Concat(saveLocation, fileName.Substring(fileName.LastIndexOf("/")+1)); //get the name of the file based on last '/'

     //now save it
     using (System.IO.FileStream outPutFile = System.IO.File.OpenWrite(fileName))
     {
          fileInformation.Stream.CopyTo(outPutFile);
          fileInformation.Stream.Close();
     }
}

 

NOTES:

There are no notes on this topic



Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading