0

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))
{
    context.Load(list.RootFolder);
    context.ExecuteQuery();
    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);
    context.ExecuteQuery();
}

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);
}
else
{
    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;
list.RootFolder.Files.Add(filecreate);
list.Update();
context.ExecuteQuery();

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

  • which version CSOM dll are you using? – Siddharth Vaghasia Apr 30 at 7:18
  • They all are latest version which have updated 4 days ago. – user11099777 Apr 30 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? – Siddharth Vaghasia Apr 30 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. – user11099777 Apr 30 at 7:45
  • File class is inside Assembly: Microsoft.SharePoint.Client (in Microsoft.SharePoint.Client.dll). Check in this dll – Siddharth Vaghasia Apr 30 at 7:58
0

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;
                     ctx.Load(web);
                     ctx.Load(web.Lists);
                     ctx.ExecuteQueryRetry();
                     List list = web.Lists.GetByTitle("NewDocLibrary");
                     ctx.Load(list);
                     ctx.ExecuteQueryRetry();
                     Folder folder = list.RootFolder.EnsureFolder("Folder1");
                     ctx.Load(folder);
                     ctx.ExecuteQueryRetry();

                     Folder folderToUpload = web.GetFolderByServerRelativeUrl(folder.ServerRelativeUrl);
                     folderToUpload.UploadFile("LargeFile.txt", "D:\\LargeFile.txt", true);
                     folderToUpload.Update();
                     ctx.Load(folder);
                     ctx.ExecuteQueryRetry();
                     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, privacy policy and cookie policy

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