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 = "[email protected]";
            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;
        }

2 Answers 2

1

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 = "[email protected]";
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.Clear();
    client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
    var response = await client.GetAsync(siteUrl + "/_api/Web");

    response.EnsureSuccessStatusCode();

    json = await response.Content.ReadAsStringAsync();
}
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

2
  • 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
    Commented Feb 20, 2018 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. Commented Feb 21, 2018 at 0:22

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.