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

How To Configure Unknown File Types For Download In IIS (ex. ".dmg" files)



TODO:

Have you ever tried to allow uses to download a file, for instance a .dmg file, but your users could not do so?  This happens because the .dmg file type is not defined in the MIME Type Settings in IIS.

 

SOLUTION:

  1. Open IIS
  2. Click on the site affected.
  3. Click "Mime Types"
  4. Click "Add"
  5. Enter ".dmg" in the file extension text box (no quotes)
  6. Enter "file/download" in the MIME type text box (no quotes)
  7. Click "OK" and you are done.  Your users will now be ble to download .dmg files from your web server.

 

NOTES:

These instructions are for IIS version 7