0

I am accessing the document from the SharePoint Doc Library from the Azure Web API Hosted on the Azure. List item i can access properly. but, when I am trying to read the document 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());
0

Try using OpenBinaryStream

context.Load(item, i => i.File);
context.ExecuteQuery();

var filedata = item.File.OpenBinaryStream();
context.ExecuteQuery();
System.IO.Stream filestream = filedata.Value;

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.