11

I need to call this REST endpoint

PATCH https://graph.windows.net/contoso.onmicrosoft.com/users/username@contoso.onmicrosoft.com?api-version=1.5 HTTP/1.1

{
    "<extensionPropertyName>": <value>
}

Please see documentation here: https://msdn.microsoft.com/en-us/library/azure/dn720459.aspx

I have the following code to set the value of one property for a user:

public async Task<ActionResult> AddExtensionPropertyValueToUser()
        {
            Uri serviceRoot = new Uri(azureAdGraphApiEndPoint);
            var token = await GetAppTokenAsync();
            string requestUrl = "https://graph.windows.net/mysaasapp.onmicrosoft.com/users/usuario1@mysaasapp.onmicrosoft.com?api-version=1.5";

            HttpClient hc = new HttpClient();
                hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

            var method = new HttpMethod("PATCH");

            var request = new HttpRequestMessage(method, requestUrl)
            {
                Content =  new StringContent("{ \"extension_33e037a7b1aa42ab96936c22d01ca338_Compania\": \"Empresa1\" }", Encoding.UTF8, "application/json")
            };

            HttpResponseMessage hrm = await hc.GetAsync(new Uri(requestUrl));
            if (hrm.IsSuccessStatusCode)
            {
                string jsonresult = await hrm.Content.ReadAsStringAsync();
                return View("TestRestCall", new SuccessViewModel
                {
                    Name = "The Title",
                    Message = "The message",
                    JSON = jsonresult.ToJson()
                });
            }
            else
            {
                return View();
            }
        }

However instead of respongint with 204 (No content), its responding with the entire user properties, so I guess something is wrong with my rest CALL

http://screencast.com/t/LmoNswKIf2

Luis Valencia
  • 32,619
  • 93
  • 286
  • 506

1 Answers1

17

I think your problem is this line:

HttpResponseMessage hrm = await hc.GetAsync(new Uri(requestUrl));

This sends an HTTP GET request to the URL that you supply, which in this case references the user "usuario1@mysaasapp.onmicrosoft.com". That is why you are seeing all the properties of the user returned in the response.

I think what you want to do is send the PATCH HttpRequestMessage that you created. To do this you need to use the SendAsync method and supply the HttpRequestMessage as a parameter. If you change the line above to the following, I think you'll set the property value and get your 204 No Content response:

HttpResponseMessage hrm = await hc.SendAsync(request);
Jimaco Brannian
  • 321
  • 3
  • 3
  • plus1 for the catch, :) – Luis Valencia May 26 '15 at 20:53
  • Hi Jimaco , I am using sendAsync , but still facing issues : Any pointers ? http://stackoverflow.com/questions/36023821/how-to-pass-the-following-json-to-a-c-sharp-patch-method-w-or-w-o-javascript-ser/36027802#36027802 – Ace McCloud Mar 16 '16 at 21:54