1

I am accessing the document from the SharePoint Doc Library from the Azure Web API Hosted on the Azure. List items I can access properly. But, when I am trying to read the document I'm getting the error message

The remote server returned an error: (401) Unauthorized.

Below is the code which I have written.

var userToken = this.Request.Headers.Authorization.Parameter;
var newToken = await GetSharePointAccessToken(doc.sharePointUrl, userToken);
using (var context = new ClientContext(doc.sharePointUrl))
{
    context.ExecutingWebRequest +=
        (s, e) => e.WebRequestExecutor.WebRequest.Headers.Add(
        "Authorization", "Bearer " + newToken);
    var web = context.Web;
    var oList = web.Lists.GetByTitle("Employee");

    ListItem item = oList.GetItemById(doc.docID);
    context.Load(item);
    context.Load(item, i => i.File);
    context.ExecuteQuery();
    FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, item["FileRef"].ToString());
    var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRef);

}

At this line I am getting the exception:

FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, item["FileRef"].ToString());

migrated from sharepoint.meta.stackexchange.com Nov 1 '17 at 13:51

This question came from our discussion, support, and feature requests site for SharePoint enthusiasts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.