2

We are trying to update fields of a ListItem using Microsoft Graph.

When we use the PATCH command in the Graph Explorer, everything works. However as soon as we want to try the same using JSON in C#, it throws back a Bad Request at us.

Below is the request that I am sending to Graph Explorer for updating the field:

PATCH https://graph.microsoft.com/v1.0/sites/mysharepoint:/sites/mySite:/lists/myList/items/1582/fields`
Content-type: application/json

{"CaseNumber": "13XX17101310238"}

The code we are trying to execute is:

string addItemJsonString = "{\"CaseNumber\":\"13XX17101310238\"}";
string requestUrl = sharepointURL + "/sites/mySharepointSiteID/lists/myList/items/1315/fields";

HttpClient client = new HttpClient();

HttpRequestMessage message = new HttpRequestMessage(new HttpMethod("PATCH"), requestUrl);
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
message.Content = new StringContent(addItemJsonString);

HttpResponseMessage response = await client.SendAsync(message);

The request message that gets sent is

Method: PATCH, 
RequestUri: 'https://graph.microsoft.com/v1.0/sites/mySite:/sites/hha:/lists/myList/items/1584/fields', 
Version: 2.0, 
Content: System.Net.Http.StringContent, 
Headers:{  
    Accept: application/json  
    Authorization: Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI6IkFRQUJBQUFBQUFCSGg0a21TX2FLVDVYcmp6eFJBdEh6Z3M3NHRlVmt2WEZiVF9lS21lR21sbk9jdlV5bnNFdEIwWGc0c2p3TUdNQTQ2NnNGRklUbXFSMjJVYWNRc1NlWVdOX1d0cXo3aFp2ZVd5MkZsZTAyenlBQSIsImFsZyI6IlJTMjU2IiwieDV0IjoiRlNpbXVGckZOb0Mwc0pYR212MTNuTlpjZURjIiwia2lkIjoiRlNpbXVGckZOb0Mwc0pYR212MTNuTlpjZURjIn0.eyJhdWQiOiJodHRwczovL2dyYXBoLm1pY3Jvc29mdC5jb20vIiwiaXNzIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvMzczZDNkODItMDdiZi00MjZiLWE4ZDItNjdhOWIzZWUwNGE1LyIsImlhdC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ml0ZS5BbGwgU2l0ZXMuU2VhcmNoLkFsbCBUYXNrcy5SZWFkIFRhc2tzLlJlYWQuU2hhcmVkIFRhc2tzLlJlYWRXcml0ZSBUYXNrcy5SZWFkV3JpdGUuU2hhcmVkIFRlcm1TdG9yZS5SZWFkLkFsbCBUZXJtU3RvcmUuUmVhZFdyaXRlLkFsbCBVc2VyLkludml0ZS5BbGwgVXNlci5SZWFkIFVzZXIuUmVhZC5BbGwgVXNlci5SZWFkQmFzaWMuQWxsIFVzZXIuUmVhZFdyaXRlIFVzZXIuUmVhZFdyaXRlLkFsbCBVc2VyVGltZWxpbmVBY3Rpdml0eS5Xcml0ZS5DcmVhdGVkQnlBcHAiLCJzdWIiOiJFQWlYZnlnSTJNV2tDeVJFTE43WDJJdGJpd0J5YlV2dU1xTWhfMXVESlo4IiwidGlkIjoiMzczZDNkODItMDdiZi00MjZiLWE4ZDItNjdhOWIzZWUwNGE1IiwidW5pcXVlX25hbWUiOiJydXNzZWxsLm1hbm5pbmdAY2FyaW5naGVhcnR6LmNvbSIsInV0aSI6Ii14b29sWW1JUTB5bjZkaXFWWVFMQUEiLCJ2ZXIiOiIxLjAifQ.eUDL7Kn6NwFRk8vOYegoAPT4HSMW2zsjlrxBHGN2o1HUk7GldCbq3ket76eW6LKChNjKgilLktB13pdC-Us5tFrQIBTxbSYnSeKFy2Jtz1aUxMtqAxD2eXcZGjE44KqUf40agD_-eqMTmDy4vSbxUEvsacin8RlSwMd94cOuxrCz3eSpdWfpsJiqsd2tkg7i7EkA9hRE9xHEyVlQR8KLzyt1Zxvoh-RyCfzKqeEfhuBUcRdgdXxx2kgqrJI1Zv7NckslDs2dc4FcZ_JhgCFc0Hzmtqq9FrwQ-1IekMG2T8Gjkb-wzNu0qwKm3ZtMPReMKQkDu61Bx-00OKu02DFO3g  
    Content-Type: text/plain; 
    charset=utf-8  
    Content-Length: 32}
}

The response that we get back is

StatusCode: 400, 
ReasonPhrase: 'Bad Request', 
Version: 1.1, 
Content: System.Net.Http.StreamContent, 
Headers:{  
    client-request-id: c0d1f2c4-e586-4094-9367-ed527dd28896  
    Strict-Transport-Security: max-age=31536000  
    request-id: c0d1f2c4-e586-4094-9367-ed527dd28896  
    Transfer-Encoding: chunked  
    x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"SouthEast Asia","Slice":"SliceA","Ring":"3","ScaleUnit":"002","Host":"AGSFE_IN_10","ADSiteName":"SIN"}}  
    Duration: 2632.4966  
    Cache-Control: private  
    Date: Tue, 27 Mar 2018 12:23:49 GMT 
    Content-Type: application/json
    }

And the response header says

client-request-id: c0d1f2c4-e586-4094-9367-ed527dd28896
Strict-Transport-Security: max-age=31536000
request-id: c0d1f2c4-e586-4094-9367-ed527dd28896
Transfer-Encoding: chunked
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"SouthEast Asia","Slice":"SliceA","Ring":"3","ScaleUnit":"002","Host":"AGSFE_IN_10","ADSiteName":"SIN"}}
Duration: 2632.4966
Cache-Control: private
Date: Tue, 27 Mar 2018 12:23:49 GMT

Any idea where are we losing it?

5
  • What is the actual error message that you're getting back? Also, what is the value of sharepointURL and accessToken? Commented Mar 26, 2018 at 16:37
  • @Marc sharepointURL contains the leading generic part of graph request up until /sites. The accessToken is the AAD token we get. The response we get is generic 400 Bad Request. We tried doing an expand using the same url and access token for the same list item and we got a successful response. However the PATCH seems to have some issues. Commented Mar 26, 2018 at 16:48
  • The error will have several pieces of information, it's a JSON response with a correlation id, error code, and message. 400 Bad Request isn't enough to go on. And I was specifically asking for the values of accessToken and sharepointURL. One or both are likely wrong. Commented Mar 26, 2018 at 17:26
  • @marc The value of the sharepointurl variable is " graph.microsoft.com/v1.0". I can paste the access token but it is freaky long and will consume the comment space here. Both the variable and access token are valid though because the same code works if I change the method from PATCH to Get and just do an Expand on the same list item I am trying to modify. The response I get back I am adding in the original post because of the space issue here in the comments. Commented Mar 27, 2018 at 12:28
  • Thanks for the headers, they're helpful. What I was really looking for is the body however. It contains a JSON description that should tell you exactly why it was a "bad request". Commented Mar 27, 2018 at 15:01

1 Answer 1

2

Your problem here is you're issuing the PATCH with a Content-Type of txt\plain. It should be application/json.

You need to change this line:

message.Content = new StringContent(addItemJsonString);

to:

message.Content = new StringContent(addItemJsonString, Encoding.UTF8, "application/json");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot @marc my friend. That solved it for us. Much appreciated :)
Glad it helps. Please make sure you mark answers on Stack Overflow that work for you as answered. It lets future readers know what worked and what didn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.