0

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?

            try
        {
            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.Accept.Clear();
            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();
            response.EnsureSuccessStatusCode();
            if(response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                lbl_result.Text += content;

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

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

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. – Mike Smith - MCT - MVP 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.