I am trying to use C# to get listitems from sharepoint online (i cannot use sharepoint.client.dll because the tools doesn't support it), hence i looking for other way like http get/ post by sharepoint rest api. the following code return 403 but it should be success. Any idea?

            var username = "xxx@company.com";
            var password = "P@ssword";

            var domain = "company.sharepoint.com";
            var handler = new HttpClientHandler();
            handler.Credentials = new System.Net.NetworkCredential(username, password, domain);

            var client = new HttpClient(handler);
            client.BaseAddress = new Uri($"https://company.sharepoint.com/sites/xxx/");
            client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");//we want to receive JSON rather than XML

            var response = await client.GetAsync("_api/lists/getbytitle('listName')/items");
            var responseText = await response.Content.ReadAsStringAsync();
                var content = await response.Content.ReadAsStringAsync();
                lbl_result.Text += content;

                var content = await response.Content.ReadAsStringAsync();
                lbl_result.Text += content;

        }catch ( Exception ex)
            lbl_result.Text = ex.Message;

You need to use Microsoft.SharePoint.Client.SharePointOnlineCredentials instead of System.Net.NetworkCredential. Here's an example:

var siteUrl = "https://contoso.sharepoint.com";

var username = "user@contoso.onmicrosoft.com";
var password = "pass@word1";
var securePassword = new SecureString();
password.ToCharArray().ToList().ForEach(c => securePassword.AppendChar(c));
var credentials = new SharePointOnlineCredentials(username, securePassword);

var handler = new HttpClientHandler();
handler.Credentials = credentials;

var uri = new Uri(siteUrl);
handler.CookieContainer.SetCookies(uri, credentials.GetAuthenticationCookie(uri));

var json = string.Empty;
using (var client = new HttpClient(handler))
    client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
    var response = await client.GetAsync(siteUrl + "/_api/Web");


    json = await response.Content.ReadAsStringAsync();

403 is "forbidden", so an issue with credentials. In SharePoint online you need to be authenticated. See this article: https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-rest-endpoints and do a search for "c# rest api sharepoint online 403" for examples.

This looks like a good example: Make a RESTful API Call to SharePoint Online from Console program

  • hi, i found that most of calling rest api example are using sharepoint client dll. This dll is not supported in my developing tool. Is there any way to make a rest api with credentials?
    – king yau
    Feb 20 '18 at 10:00
  • @kingyau Using the REST API does not require any client DLL. It's just a URL to a resource. Data is sent and returned in XML or JSON format. What you saw was probably using a DLL to get the credentials. I'll look around and see if I can find a better example for you. Feb 21 '18 at 0:22

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.