首页 > 代码库 > Dynamics CRM 2015/2016/365 Web API:批处理任务

Dynamics CRM 2015/2016/365 Web API:批处理任务

Web API为我们提供的批量任务执行功能,我们可以在一个请求中混合多个不相干的创建查询请求,并且其还提供了事务功能,如果在事务中如果有脚本出现了错误,则其提供回滚功能。


如下是批处理的请求报文,在报文里面我们需要设置批处理任务的编号,因为我们需要用它来划分任务的开始和结束边界。另外,我们还可以通过changeset标记来划分事务,在同一个changeset中,如果有处理失败了,那么整个changeset都会被回滚。

POST https://crmhomedev06.api.crm6.dynamics.com/api/data/v8.2/$batch HTTP/1.1
Accept: application/json
OData-MaxVersion: 4.0
OData-Version: 4.0
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlJyUXF1OXJ5ZEJWUldtY29jdVhVYjIwSEdSTSIsImtpZCI6IlJyUXF1OXJ5ZEJWUldtY29jdVhVYjIwSEdSTSJ9.eyJhdWQiOiJodHRwczovL2NybWhvbWVkZXYwNi5hcGkuY3JtNi5keW5hbWljcy5jb20vIiwiaXNzIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvYmI4Y2IyMmUtY2VhOC00ZTQwLWJiM2ItNmIxZGEwMzg4MDEwLyIsImlhdCI6MTQ3OTM1ODkyNiwibmJmIjoxNDc5MzU4OTI2LCJleHAiOjE0NzkzNjI4MjYsImFjciI6IjEiLCJhbXIiOlsicHdkIl0sImFwcGlkIjoiNTYzOTI3NDktZDlmOS00NzZmLWIyM2QtZmE3ZGZkYjdlZjQzIiwiYXBwaWRhY3IiOiIwIiwiZV9leHAiOjEwODAwLCJmYW1pbHlfbmFtZSI6Inhpb25nIiwiZ2l2ZW5fbmFtZSI6ImplZmYiLCJpcGFkZHIiOiIyMDcuNDYuMTM5LjI0OSIsIm5hbWUiOiJqZWZmIHhpb25nIiwib2lkIjoiNTg3ODk2NGUtNzVhMy00MmE5LTkyMTItMGY2YWFkMGEyN2U5IiwicGxhdGYiOiIzIiwicHVpZCI6IjEwMDM3RkZFOUJEMEY2QTIiLCJzY3AiOiJ1c2VyX2ltcGVyc29uYXRpb24iLCJzdWIiOiI0U3A4Z2htQUdBSlNvQnpJOFM0M1RJWmxzbHZQNTRFdFpxTEh3Qm44Y0ZjIiwidGlkIjoiYmI4Y2IyMmUtY2VhOC00ZTQwLWJiM2ItNmIxZGEwMzg4MDEwIiwidW5pcXVlX25hbWUiOiJqZWZmLnhpb25nQGNybWhvbWVkZXYwNi5vbm1pY3Jvc29mdC5jb20iLCJ1cG4iOiJqZWZmLnhpb25nQGNybWhvbWVkZXYwNi5vbm1pY3Jvc29mdC5jb20iLCJ2ZXIiOiIxLjAiLCJ3aWRzIjpbIjYyZTkwMzk0LTY5ZjUtNDIzNy05MTkwLTAxMjE3NzE0NWUxMCJdfQ.I_j1DNlvKJcEuEziVCQ83hF3j5FsJkBFwvQY9SvdcVQTOEZOq69_X7GqfDOnEvOi58dZCCiCcJW2N73PY9QgxIKxOSJ3FBJfl4c6OywcDfgLSMUJ9HScXC3HTHO8pnoQwREl9b83DHN3wqAeQudVs-hMH7-3vtpV95Ro0vNGcaw2zvQjDxm9ajIo5h7oq12fWLQe40di5dOeokokxBAqNUFP3huo_Z3w4o-TYPZERg3fBo_ErwGnvWinnJxBXjJIvJy9o5RSa6RLopITdXIWfr6s5-jTEtrr5MGhwqs3hzKPlrffpC85LP98mhGQLpg2fZD-aB96x_uWmHi_SwGT7A
Content-Type: multipart/mixed; boundary=batch_AAA123
Host: crmhomedev06.api.crm6.dynamics.com
Content-Length: 1105
Expect: 100-continue

--batch_AAA123
Content-Type: multipart/mixed;boundary=changeset_BBB456

 --changeset_BBB456
Content-Type: application/http
Content-Transfer-Encoding:binary
Content-ID: 1

 POST https://crmhomedev06.crm6.dynamics.com/api/data/v8.2/tasks HTTP/1.1
 Content-Type: application/json;type=entry

 {‘subject‘:‘Task 1 in batch‘,‘regardingobjectid_account_task@odata.bind‘:‘https://crmhomedev06.crm6.dynamics.com/api/data/v8.2/accounts(5df6e552-bca5-e611-8100-1458d05b1178)‘}
--changeset_BBB456
Content-Type: application/http
Content-Transfer-Encoding:binary
 Content-ID: 2

 POST https://crmhomedev06.crm6.dynamics.com/api/data/v8.2/tasks HTTP/1.1
Content-Type: application/json;type=entry

 {‘subject‘:‘Task 2 in batch‘,‘regardingobjectid_account_task@odata.bind‘:‘https://crmhomedev06.crm6.dynamics.com/api/data/v8.2/accounts(5df6e552-bca5-e611-8100-1458d05b1178)‘}
-changeset_BBB456--

--batch_AAA123
Content-Type: application/http
Content-Transfer-Encoding:binary
GET https://crmhomedev06.crm6.dynamics.com/api/data/v8.2/accounts HTTP/1.1
Accept: application/json

--batch_AAA123--

批处理任务响应报文

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: multipart/mixed; boundary=batchresponse_c01b10e0-01f1-4b2b-8fb9-390b7d8166d4
Expires: -1
Server: Microsoft-IIS/8.5
REQ_ID: 764a9d7b-17be-4ebe-98c1-acb21c1e68ba
Set-Cookie: ReqClientId=c4e5658f-bd22-4e41-9328-2e4481d75142; expires=Wed, 17-Nov-2066 06:43:07 GMT; path=/; secure; HttpOnly
OData-Version: 4.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 17 Nov 2016 06:43:15 GMT
Content-Length: 342441
Strict-Transport-Security: max-age=31536000; includeSubDomains

--batchresponse_c01b10e0-01f1-4b2b-8fb9-390b7d8166d4
Content-Type: multipart/mixed; boundary=changesetresponse_10c4be12-1f1e-4d5a-9cbf-51bb6c390615

--changesetresponse_10c4be12-1f1e-4d5a-9cbf-51bb6c390615
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

HTTP/1.1 204 No Content
OData-Version: 4.0
Location: https://crmhomedev06.crm6.dynamics.com/api/data/v8.2/tasks(9a932b1d-91ac-e611-80f9-1458d05a2ab0)
OData-EntityId: https://crmhomedev06.crm6.dynamics.com/api/data/v8.2/tasks(9a932b1d-91ac-e611-80f9-1458d05a2ab0)


--changesetresponse_10c4be12-1f1e-4d5a-9cbf-51bb6c390615
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 2

HTTP/1.1 204 No Content
OData-Version: 4.0
Location: https://crmhomedev06.crm6.dynamics.com/api/data/v8.2/tasks(9b932b1d-91ac-e611-80f9-1458d05a2ab0)
OData-EntityId: https://crmhomedev06.crm6.dynamics.com/api/data/v8.2/tasks(9b932b1d-91ac-e611-80f9-1458d05a2ab0)


--changesetresponse_10c4be12-1f1e-4d5a-9cbf-51bb6c390615--
--batchresponse_c01b10e0-01f1-4b2b-8fb9-390b7d8166d4
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=http://www.mamicode.com/minimal>
使用这种方式批量执行脚本的缺陷是我们需要自己去解析返回的报文,这可能需要我们花费不少功夫。

Dynamics CRM 2015/2016/365 Web API:批处理任务