Question

I am trying to download a document from a SharePoint document library using MVC however whenever I try to run my code I get the error mentioned above. I am new to SharePoint so please be nice. here is my code:

Web helper:

public Stream DownloadDocument(string SiteURL, string documentName)
{
  ListItem item = GetDocumentFromSP(documentName);

  if (item != null) {
    using (ClientContext clientContext = new ClientContext(SiteUrl)) {
      FileInformation fileInformation = 
        Microsoft.SharePoint.Client.File.OpenBinaryDirect(
          clientContext, 
          item["My Document.docx"].ToString()
        );

      return fileInformation.Stream;
    }
  }

  return null;
}

Controller:

public ActionResult Index() 
{
  Stream documentDownload = 
    WebHelper.DownloadDocument(
      "http://MySharePointServer/Docs/Forms/AllItems.aspx", 
      "My Document"
  );

  model.downloadedDoc = documentDownload;

  return view(model)
}
Was it helpful?

Solution

This line:

Microsoft.SharePoint.Client.File.OpenBinaryDirect(
          clientContext, 
          item["My Document.docx"].ToString()

Is wrong, the proper syntax is to specify the Server Relative URL to the file:

public static FileInformation OpenBinaryDirect(
    ClientContext context,
    string serverRelativeUrl
)

Your line should look something like this:

Microsoft.SharePoint.Client.File.OpenBinaryDirect(
      clientContext, 
      "/My Document.docx"

David Sterling - http://davidmsterling.blogspot.com - http://www.sterling-consulting.com - http://www.sharepoint-blog.com

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top