0

I would download files from document library recursively and bundle them to ZIP. I tried to achieve in the below code. However I get an error in OpenBinaryDirect as "The remote server returned an error: (401) Unauthorized.".

Microsoft.SharePoint.Client.File fl = splistItem.File;
var stream = fl.OpenBinaryStream();
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(Context, fl.ServerRelativeUrl);
var fileName = Path.Combine(@"C:\Users\user\Documents\", fl.Title);
using (var fileStream = System.IO.File.Create(fileName))
{
    fileInfo.Stream.CopyTo(fileStream);
}

What should be the possible error? Guess it is a credentials issue, however not particularly sure on this. Any thoughts would be helpful.

0

Set the credential for context.

sample code:

online:

    string siteUrl = "https://xxx.sharepoint.com/sites/TST";
                string userName = "lee@xxx.onmicrosoft.com";
                string password = "pw";

                using (var ctx = new ClientContext(siteUrl))
                {
                    SecureString securePassword = new SecureString();
                    foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
                    ctx.Credentials = new SharePointOnlineCredentials(userName, securePassword);
//read file
}

on-premise:

NetworkCredential credentials = new NetworkCredential("username", "pwd", "domain");

ClientContext context = new ClientContext(“http://thesitecollection”);

context.Credentials = credentials;

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.