1

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_");
            }
        }
    }

2 Answers 2

1

I solved the problem. here is code

Microsoft.SharePoint.Client.File file = GetTheFileFromTheDocLibrary("myFile.xlsx");
ClientContext clientContext = new ClientContext(Session["SPHostUrl"].ToString());
FileInformation fInfo = SP.File.OpenBinaryDirect(clientContext, file.ServerRelativeUrl);

GetTheFileFromTheDocLibrary returns File.

2
  • 3
    This actually works. I have no idea why, but it feels wrong. You can do something like this: var clientContextHack = new ClientContext(clientContext.Url). I can fetch the file through clientContext.Web.GetFileByServerRelativeUrl, but not OpenBinaryDirect without the hack.
    – eirikb
    Commented Mar 31, 2014 at 9:34
  • Recreating a clientcontext only works in debug mode, or console app. In an app, the authentication won't work. we have to use the context.Web.GetFileByServerRelativeUrl.... Commented Nov 13, 2014 at 11:46
0

You should put the right permission for you app. (e.g. Full Control on the Web object or just on your list Documents if you want to be more selective).

3
  • i already have Full Control permission on Web and Documents
    – Sage Shah
    Commented Jun 10, 2013 at 14:28
  • Are you developing an Autohosted App or a Provider-hosted App? Have you tried to comment the last two lines (with the openbinarydirect) to see if the problem comes from there? Otherwise could you debug your app and tell us where exactly the problem is coming from (line number).
    – ggobbe
    Commented Jun 11, 2013 at 16:23
  • I am using Provider Hosted Application. However i have solved the problem. Rather than getting client context via AccessToken i used following line "ClientContext cc = new ClientContext(SPHostUrl);"
    – Sage Shah
    Commented Jun 11, 2013 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.