0

I managed to upload files less than 2mb, but since I also want to upload larger files I had to move away from CSOM.

So I searched the web and found this solution:

ClientContext ctx = new ClientContext("https://mysharepoint.com/sites/testing/");
ctx.Credentials = new SharePointOnlineCredentials("user@sharepoint.com", passWord);
Folder folder = ctx.Web.GetFolderByServerRelativeUrl("test");
string file = String.Concat(Environment.CurrentDirectory, @"\a.pdb");

List docLib = ctx.Web.Lists.GetByTitle("test");
ctx.Load(docLib);
ctx.ExecuteQuery();

using (MemoryStream stream = new MemoryStream(System.IO.File.ReadAllBytes(file)))
{
    Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, "/test/a.pdb", stream, true);
}

but it exits with the message:

401 not authorized

Seems to be a known issue in current SharePoint versions, but i also saw on other posts here, that it got fixed a while ago and should work?

  • Can you try this Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, "/sites/testing/test/a.pdb", stream, true); let me still you get error – vinit kumar Sep 5 '18 at 1:24
0

The following code for your reference.

string targetSiteURL = @"https://mysharepoint.com/sites/testing/";

var login = "lz@xxx.onmicrosoft.com";
var password = "password";

var securePassword = new SecureString();
foreach (char c in password)
{
    securePassword.AppendChar(c);
}

ClientContext ctx = new ClientContext(targetSiteURL);
ctx.Credentials = new SharePointOnlineCredentials(login, securePassword);

string file = @"c:\temp\a.pdb";

using (MemoryStream stream = new MemoryStream(System.IO.File.ReadAllBytes(file)))
{
    Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, "/sites/testing/test/a.pdb", stream, true);
}

And check if the login account and password are right.

If you use SharePoint On-premise, we need using the below code to pass Credentials.

ctx.Credentials=new NetworkCredential("username", "password","domain");

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.