I am writing a web api code to upload documents in specific folder using csom code using savebinarydirect method, which is failing

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/262634

Domanda

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.

È stato utile?

Soluzione

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..!!!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top