将 primarycontactid 关联到 Case-Dynamics CRM 网站 API
Associate primarycontactid to Case-Dynamics CRM web API
我正在尝试创建个案并在 primarycontactId 字段中关联联系人。我正在使用备用键来查找联系人,如下所示。
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",
"primarycontactid@odata.bind": "https://xxxxx.crm.dynamics.com/api/data/v8.2/contacts(xxi_xxx='2533274975913147')"
}
代码如下
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;
}
然而我得到:
An unexpected error occurred.","innererror":{
"message":"An unexpected error occurred.","type":"System.ServiceModel.FaultException.","type":"System.ServiceModel.FaultException1[[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\
有没有人以前遇到过这种情况?
我不确定 Web API 是否允许通过备用键填充查找字段。
这两个步骤应该有效...
- 通过备用键检索联系人的 GUID
- 使用 ID 而不是备用键填充查找,即:
"primarycontactid@odata.bind": "/contacts(F56D5D25-8B0D-E711-8104-00155D6FD705)"
你搞混了。验证这一点并更正架构名称。
事件有 2 个可选的联系人查找,模式名称是 primarycontactid 和 responsiblecontactid。
primarycontactid@odata.bind
但事件有一个强制性 客户查找和架构名称是customerid
。没有这个事件就无法创建。通过这个,请求就会成功。
customerid_contact@odata.bind
在请求正文中试试这个,它会有所帮助。
"primarycontactid@odata.bind":"/contacts(alternate_key_field='2222222')"
我正在尝试创建个案并在 primarycontactId 字段中关联联系人。我正在使用备用键来查找联系人,如下所示。
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",
"primarycontactid@odata.bind": "https://xxxxx.crm.dynamics.com/api/data/v8.2/contacts(xxi_xxx='2533274975913147')"
}
代码如下
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;
}
然而我得到:
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\
有没有人以前遇到过这种情况?
我不确定 Web API 是否允许通过备用键填充查找字段。
这两个步骤应该有效...
- 通过备用键检索联系人的 GUID
- 使用 ID 而不是备用键填充查找,即:
"primarycontactid@odata.bind": "/contacts(F56D5D25-8B0D-E711-8104-00155D6FD705)"
你搞混了。验证这一点并更正架构名称。
事件有 2 个可选的联系人查找,模式名称是 primarycontactid 和 responsiblecontactid。
primarycontactid@odata.bind
但事件有一个强制性 客户查找和架构名称是customerid
。没有这个事件就无法创建。通过这个,请求就会成功。
customerid_contact@odata.bind
在请求正文中试试这个,它会有所帮助。
"primarycontactid@odata.bind":"/contacts(alternate_key_field='2222222')"