Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use SaveBinaryDirect with Sharepoint online always return 403

i am new with SharePoint,

I am trying to upload a picture inside a document library by c# code.

There is my code

string webUrl = "https://mysharepointesite.sharepoint.com/";



  string targetSite = webUrl;
  using (ClientContext ctx = ClaimClientContext.GetAuthenticatedContext(targetSite))
  {
    if (ctx != null)
    {
      var lib = ctx.Web.Lists.GetByTitle("mesdocuments");
      ctx.Load(lib);
      ctx.Load(lib.RootFolder);
      ctx.ExecuteQuery();
      string sourceUrl = "C:\\temp\\picture.jpg";          
      using (FileStream fs = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read))
      {
        Microsoft.SharePoint.Client.File.SaveBinaryDirect (ctx, lib.RootFolder.ServerRelativeUrl + "/myPicture.jpg", fs, true);
      }
    }

But i always get an 403 error.

I also verify if it was not the same issue has mentionned in this post, but i don't think it is.

https://razirais.wordpress.com/2011/05/19/how-to-fix-sharepoint-online-403-forbidden-error-while-downloading-files-using-client-object-model/

When i do it i received a 405 error, and the fiddler log show me that the request was trying to redirect the context to the authentication page.

it's there a setting i need to enable ? Any idea what is my trouble ?

like image 323
Cédric Boivin Avatar asked Nov 14 '25 22:11

Cédric Boivin


1 Answers

Since you have tagged it in SharePoint online, I am assuming that you are using SharePoint online only.

You need to pass the user name and password to the ClientContext. So, modify your code as below:

string webUrl = "https://mysharepointesite.sharepoint.com/";

string userName = "[email protected]";
string password = "password";

using (ClientContext ctx = new ClientContext(webUrl))
{
    SecureString securePassword = new SecureString();
    foreach (char c in password.ToCharArray())
    {
        securePassword.AppendChar(c);
    }   

    ctx.AuthenticationMode = ClientAuthenticationMode.Default;
    ctx.Credentials = new SharePointOnlineCredentials(userName, securePassword);

    if (ctx != null)
    {
      var lib = ctx.Web.Lists.GetByTitle("mesdocuments");
      ctx.Load(lib);
      ctx.Load(lib.RootFolder);
      ctx.ExecuteQuery();
      string sourceUrl = "C:\\temp\\picture.jpg";          
      using (FileStream fs = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read))
      {
        Microsoft.SharePoint.Client.File.SaveBinaryDirect (ctx, lib.RootFolder.ServerRelativeUrl + "/myPicture.jpg", fs, true);
      }
    }

}

Also, ensure that you are using the correct CSOM dll i.e using the SharePoint online CSOM SDK. Looking at your current implementation, i think you are using the client context incorrectly which is causing the error.

You can download the latest SDK from the link mentioned below or using Nuget (preferred).

If you download the SDK, then you might need to manually reference the dlls from the folder where it is installed.

Usually, if installed in the C: drive, the location is:

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI.

from there, you need to reference the Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll files in your project.

Download link - SharePoint Online Client Components SDK

Nuget package - Microsoft.SharePointOnline.CSOM

Also, if you are not a fan of passing user name and password, you can use client id and client secret. To use that, you can take a look at my answer here

like image 147
Gautam Sheth Avatar answered Nov 17 '25 20:11

Gautam Sheth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!