I can upload the documents using a console app, but when using the same code in. Net Web API, I am getting an error saying, "Microsoft.Sharepoint.Client.File" doesn't contain a method "Savebinarydirect". The DLLs which I'm using in Web API for CSOM are:-

  1. Microsoft.Sharepoint.Client.Portable
  2. Microsoft.SharePoint.Client.runtime.Portable
  3. Microsoft.Sharepoint.Client.Runtime.Windows

The code is running fine in the console application. Can anyone please help?

CSOM code I was using :

using (var filestream = new FileStream(filename, FileMode.Open))
    var fileinfo = new FileInfo(filename);
    var result = Path.GetFileName(filename);
    var fileUrl = String.Format("{0}/{1}/", list.RootFolder.ServerRelativeUrl, caseno);
    //Microsoft.SharePoint.Client.File newfile = list.RootFolder.Files.Add(fileUrl + result);
    Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fileUrl + result, filestream, true);

This code is working for the console but not for Web API. I am trying one more CSOM code :

string uploadLocation = string.Empty;
if (!string.IsNullOrEmpty(caseno))
    uploadLocation = string.Format("/{0}/{1}/{2}/", Url,"NewDocLibrary", caseno);
    uploadLocation = string.Format("/{0}/", "NewDocLibrary");
FileCreationInformation filecreate = new FileCreationInformation();
byte[] bytefile = System.IO.File.ReadAllBytes(filename);
filecreate.Content = bytefile;
filecreate.Overwrite = true;

uploadLocation = uploadLocation + Path.GetFileName(filename);
filecreate.Url = uploadLocation;

It is giving me an error "server relative URLs must start with spweb.serverrelativeurl" in my console.

  • which version CSOM dll are you using? Commented Apr 30, 2019 at 7:18
  • They all are latest version which have updated 4 days ago. Commented Apr 30, 2019 at 7:37
  • still please share exact version of Microsoft.Sharepoint.Client.File, also can you share screenshot whether it is compile time error or runtime? Commented Apr 30, 2019 at 7:39
  • version -16.1.8810.1200 . this is the dll verion I am using for Microsoft.Sharepoint.Client.Portable.dll. Microsoft.Sharepoint.Client.File doesn't even contain "SaveBinaryDirect" method under dll Microsoft.Sharepoint.Client.Portable.dll. Commented Apr 30, 2019 at 7:45
  • File class is inside Assembly: Microsoft.SharePoint.Client (in Microsoft.SharePoint.Client.dll). Check in this dll Commented Apr 30, 2019 at 7:58

1 Answer 1


I checked this issue in detail... SaveDirectBinary method is not available in File Class which is in Microsoft.SharePoint.Client.Portable.

Below is screenshot of File class from Microsoft.SharePoint.Client.Portable.

enter image description here

You shoul use SaveBinary method only if Microsoft.SharePoint.Client.dll can't be used in Web API(please share what error you are getting while reference above client Dll.

Why it might be working in CSOM is because you would have been using Microsoft.SharePoint.Client.dll in console app.

Screen shot from Client.dll enter image description here

Update to question to upload file using CSOM Ref link - https://www.sharepointpals.com/post/upload-file-to-sharepoint-office-365-programmatically-using-c-csom-pnp/

                     Web web = ctx.Web;
                     List list = web.Lists.GetByTitle("NewDocLibrary");
                     Folder folder = list.RootFolder.EnsureFolder("Folder1");

                     Folder folderToUpload = web.GetFolderByServerRelativeUrl(folder.ServerRelativeUrl);
                     folderToUpload.UploadFile("LargeFile.txt", "D:\\LargeFile.txt", true);
                     folderToUpload.EnsureProperty(f => f.ServerRelativeUrl);
                     var serverRelativeUrl = folderToUpload.ServerRelativeUrl.TrimEnd('/') + '/' + "LargeFile.txt";

hope this helps...Happy coding..!!!


Your Answer

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

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