I am using C# HttpClient to query odata from Sharepoint. This works wonderfully if I am accessing a list.

I generate the SharePointOnlineCredentials to get authentication cookie and then I pass the full URL to this method:

HttpResponseMessage response = client.GetAsync(fullUrl).Result;

For lists, I always get the proper response back as json that I then parse out. For Document Libraries, this doesn't work... everything I read indicates that it should, but I always get a 404 response.

My actual Sharepoint api url looks like this:


If I run that URL in a chrome browser, it works for both lists and document libraries, but when I run that same exact URL in the C# HttpClient, it only works for lists... for some reason I get 404 for document library...

On a somewhat related note, I get "Bad Request" errors if I attempt to use any ODATA filters on a list or document library... but again, the filters work great in the browser... just not the C# HttpClient code...

Does anyone have ideas for what I can try? I'm at my wits end...

  • It works on the title of the doc lib, in case of Shared Documents it usually is titled as Documents. To find the title, go to the doc lib settings and then click on the List name, desc and navigation link. You will find your title there which you can use in the API Nov 6, 2018 at 18:51
  • right, that's what I am doing, where I put <listName> above is where I plug in the document library name... again, it works fine in the browser... just tells me the path doesn't exist in C# client for some reason... I use an identical path in both cases, but C# is doing something under the covers that I can't quite figure out...
    – eerick
    Nov 6, 2018 at 20:17

2 Answers 2


The following C# code for your reference.

static void Main(string[] args)
    //Creating Password
    string webUrl = "https://lz.sharepoint.com/sites/lz";
    string USER = "[email protected]";
    string PWD = "password";
    string listName = "Documents";
    string RESTURL = "{0}/_api/web/lists/getbytitle('{1}')/items";

    Task<string> result = getListItems(webUrl,listName,USER,PWD,RESTURL);

private static async Task<string> getListItems(string webUrl, string listName,string USER, string PWD,string RESTURL)
    //Creating Credentials
    var passWord = new SecureString();
    foreach (var c in PWD) passWord.AppendChar(c);
    var credential = new SharePointOnlineCredentials(USER, passWord);

    //Creating Handler to allows the client to use credentials and cookie
    using (var handler = new HttpClientHandler() { Credentials = credential })
        //Getting authentication cookies
        Uri uri = new Uri(webUrl);
        handler.CookieContainer.SetCookies(uri, credential.GetAuthenticationCookie(uri));

        //Invoking REST API
        using (var client = new HttpClient(handler))
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.GetAsync(string.Format(RESTURL, webUrl,listName)).ConfigureAwait(false);

            string jsonData = await response.Content.ReadAsStringAsync();

            return jsonData;
  • so this is exactly what my code already looks like... this works perfectly fine for any List, but NOT for Document Libraries... everything I read says they are the same thing... but there MUST be something different if I keep getting 404 for the Document Libraries... are you able to use this code for a Doc Library? Do I maybe need to configure something about the library differently? Again, I can access both Lists and Document Libraries in a browser using the REST endpoints, but something about initiating the connection in C# causes Doc Libraries to return 404
    – eerick
    Nov 7, 2018 at 16:57

I found the answer... it comes down to a design decision by Microsoft and honestly it is rather stupid.

I was seeing 404 on the Document Library because the account I was using didn't have permissions for the Library. What confused me was the 404 response... normally if I don't have permissions I get a 403 "Forbidden" error... so I expected that a permissions issue would yield that same result for this Doc Library. I forgot I was using a different account for accessing the site than the one I was using in C# for testing.

So the answer comes down to this: Microsoft SharePoint hides sites if you don't have permissions to view them... so when you hit the URL in C# you get a 404 "Not Found" error... but when you hit them in a browser you get told you have to request permissions... that's extremely misleading... a 404 and 403 are NOT the same thing.

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.