I am trying to create a case and associate a contact in the primarycontactId field. I am using an alternate key to look up the contact as below.
POST https://xxxxx.crm.dynamics.com/api/data/v8.2/incidents HTTP/1.1
Accept: application/json
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Host: xxxxxx.crm.dynamics.com
Content-Length: 161
{
"title": "case101",
"[email protected]": "https://xxxxx.crm.dynamics.com/api/data/v8.2/contacts(xxi_xxx='2533274975913147')"
}
Code as below
using (HttpClient httpClient = new HttpClient())
{
var method2 = new HttpMethod("POST");
Uri requesturi = new Uri(string.Format("{0}/api/data/v8.2/", url));
httpClient.BaseAddress = requesturi;
httpClient.Timeout = new TimeSpan(0, 0, 4); // 10 minutes
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
result = GetS2SAccessToken(url, pwd);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result);
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
HttpRequestMessage request = new HttpRequestMessage(method,string.Format( "contacts(xxxxx_xxxx='{0}')",xxxx.ToString()));
request.Content = new StringContent(props.ToString(), Encoding.UTF8, "application/json");
HttpRequestMessage request2 = new HttpRequestMessage(method2, string.Format("incidents"));
request2.Content = new StringContent(props1.ToString(), Encoding.UTF8, "application/json");
HttpResponseMessage createResponse1;
createResponse1 = await httpClient.SendAsync(request2);
return createResponse1.Content;
}
However I get:
An unexpected error occurred.","innererror":{ "message":"An unexpected error occurred.","type":"System.ServiceModel.FaultException.","type":"System.ServiceModel.FaultException
1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Crm.Extensibility.OData.CrmODataServiceDataProvider.CreateEdmEntity(CrmODataExecutionContext context, String edmEntityName, EdmEntityObject entityObject, Boolean isUpsert)\r\n at Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]","stacktrace":" at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Create(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode, Dictionary
2 optionalParameters)\r\n at Microsoft.Crm.Extensibility.OData.CrmODataExecutionContext.Create(Entity entity)\r\n at Microsoft.Crm.Extensibility.OData.EntityController.PostEntitySet(String entitySetName, EdmEntityObject entityObject)\r\
Has anyone experienced this before?