Hi all
I am looking at using the REST endpoints via c#, I am able to create entities by serializing an account object and posting it to the web service endpoint, but I'm having trouble working out how to do so for the single navigation properties. If I was building the JSON manually I'd be able to reference parentaccountid@odata.bind but how do I do so when serializing the object? I can't include a @ or . character in a class property, obviously.
This is what I am currently trying
using (HttpClient httpClient = new HttpClient())
{
// Define the Web API address of the service and the period of time each request has to execute.
httpClient.BaseAddress = new Uri(config.ServiceUrl);
httpClient.Timeout = new TimeSpan(0, 2, 0); // 2 minutes
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
// Set the type of payload that will be accepted.
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
#region Create a entity
// Create an in-memory account using the early-bound Account class.
Account account = new Account();
account.name = "Contoso";
account.telephone1 = "555-5555";
account.parentaccountid = "/api/data/v8.1/accounts(B408010B-852B-E611-80DE-C4346BC5C378)";
// It is a best practice to refresh the access token before every message request is sent. Doing so
// avoids having to check the expiration date/time of the token. This operation is quick.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", auth.AcquireToken().AccessToken);
// Send the request, and then check the response for success.
// POST api/data/accounts
HttpResponseMessage response =
await HttpClientExtensions.SendAsJsonAsync<Account>(httpClient, HttpMethod.Post, "api/data/v8.1/accounts", account);
if (response.IsSuccessStatusCode)
Console.WriteLine("Account '{0}' created.", account.name);
else
throw new Exception(String.Format("Failed to create account '{0}', reason is '{1}'.",
account.name, response.ReasonPhrase), new CrmHttpResponseException(response.Content));
But I get this message
A 'PrimitiveValue' node with non-null value was found when trying to read the value of the navigation property 'parentaccountid'; however, a 'StartArray' node, a 'StartObject' node, or a 'PrimitiveValue' node with null value was expected.
Any ideas how I can do this?