将primarycontactid与Case-Dynamics CRM Web API相关联

时间:2017-07-28 00:27:38

标签: c# dynamics-crm dynamics-crm-webapi

我正在尝试创建一个案例并在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;
         }

但是我得到了:

  

发生意外错误。"," innererror":{         " message":"发生意外错误。","输入":" System.ServiceModel.FaultException。",&#34 ;在Microsoft的Microsoft.Crm.Extensibility.OData.CrmODataExecutionContext.Create(实体实体)\ r \ n中键入":#34; 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。 Crm.Extensibility.OData.EntityController.PostEntitySet(String entitySetName,EdmEntityObject entityObject)\ r \

以前有没有人经历过这个?

3 个答案:

答案 0 :(得分:0)

我不确定Web API是否允许通过备用密钥填充查阅字段。

这个两步过程应该有效......

  1. 按其备用密钥
  2. 检索联系人的GUID
  3. 使用ID而不是备用密钥填充查找,即:
  4. "primarycontactid@odata.bind": "/contacts(F56D5D25-8B0D-E711-8104-00155D6FD705)"

答案 1 :(得分:0)

你搞砸了。验证这个&更正架构名称。

事件有2个可选的联系人查找&架构名称是primarycontactid& responsiblecontactid。

primarycontactid@odata.bind

但事件有强制性客户查询&架构名称为customerid。没有这个事件就无法创建。通过这个,请求将成功。

customerid_contact@odata.bind

答案 2 :(得分:0)

在请求正文中试试这个,它会有所帮助。

server {
   server_name admin.example.com;
   index index.php index.html index.htm index.nginx-debian.html;
   root /var/www/example/apps/dashboard/public;

   try_files $uri $uri/ /index.php$is_args$args;

   access_log /var/log/nginx/example_access.log;
   error_log  /var/log/nginx/example_error.log;

   location ~ \.php$ {
     include snippets/fastcgi-php.conf;
     fastcgi_pass unix:/run/php/php7.4-fpm.sock;
   }

   location ~ /\.ht {
     deny all;
   }

   # pass the PHP scripts to FastCGI server listening on /var/run/php7.4-fpm.sock
   location ~ \.php$ {
           try_files $uri /index.php =404;
           fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
           fastcgi_index index.php;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
   }

   listen 443 ssl; # managed by Certbot
   ssl_certificate /etc/letsencrypt/live/admin.example.com/fullchain.pem; # managed by Certbot
   ssl_certificate_key /etc/letsencrypt/live/admin.example.com/privkey.pem; # managed by Certbot
   include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
   ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = admin.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    listen 80;
    return 404; # managed by Certbot
}