I am getting following exception "The remote server returned an error: (401) Unauthorized." While i'm trying to access a file on SharePoint Document.
code:
private void sync2(string filename)
{
Uri hostWeb = new Uri(Session["SPHostUrl"].ToString());
using (var clientContext = TokenHelper.GetS2SClientContextWithWindowsIdentity(hostWeb, Request.LogonUserIdentity))
{
// Getting list of Documents
List sharedDocumentsList = clientContext.Web.Lists.GetByTitle("Documents");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
"<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>" +
filename + "</Value></Eq></Where><RowLimit>1</RowLimit></Query></View>";
ListItemCollection listItems = sharedDocumentsList.GetItems(camlQuery);
clientContext.Load(sharedDocumentsList);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
if (listItems.Count == 1)
{
SP.ListItem item = listItems[0];
string cc = (string)item["FileRef"];
FileInformation fileInformation = SP.File.OpenBinaryDirect(clientContext, (string)item["FileRef"]);
bool a = FileExists("exp_");
}
}
}