0
votes

I have UWP App and currently working with Microsoft Graph API. I am constantly running into issue.

The issue is when I tried to create ListItem at sharepoint, it throws an error.

StatusCode: 400, ReasonPhrase: 'Bad Request'

My code is as below

    string accessToken = GlobalSettings.userToken;
string addItemJsonString = "{\"fields\":{\"Title\":\"This is test record\"}}";
string requestUrl = "https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items";

HttpClient client = new HttpClient();

HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, requestUrl);
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
message.Content = new StringContent(addItemJsonString, Encoding.UTF8, "application/json"); 
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
HttpResponseMessage response = await client.SendAsync(message);

I followed this link https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/listitem_create#request-body

Any help would be appreciable. :)

Thanks

1
Can you provide the full error message with client request id and timestamp. Or a fiddler request (without the bearer token) would be great too. - Dan Kershaw - MSFT
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{ client-request-id: 77f2e624-80f5-48fb-8340-5dc5ee510d92 request-id: 77f2e624-80f5-48fb-8340-5dc5ee510d92 Transfer-Encoding: chunked x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"SouthEast Asia","Slice":"SliceA","Ring":"3","ScaleUnit":"001","Host":"AGSFE_IN_11","ADSiteName":"SIN"}} Duration: 1396.6031 Cache-Control: private Date: Fri, 15 Dec 2017 05:51:09 GMT Content-Type: application/json}} - Mobeen

1 Answers

0
votes

For you shouldnt write string requestUrl = "https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items"; that microsoft want you to replace the {site-id} and {list-id} to yours.

And I think your addItemJsonString can use Json.Net to convert a class to string that hard code may be negligence some character.

Sites - Documentation - Microsoft Graph