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

Posted ghostbear

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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‘,‘[email protected]‘:‘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‘,‘[email protected]‘:‘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=minimal
OData-Version: 4.0

{
  "@odata.context":"https://crmhomedev06.crm6.dynamics.com/api/data/v8.2/$metadata#accounts","value":[
    {
      "@odata.etag":"W/\"646599\"","openrevenue_date":"2016-11-16T12:31:53Z","territorycode":1,"address2_freighttermscode":1,"address2_shippingmethodcode":1,"accountid":"9381b8b5-55a6-e611-80f9-1458d05a2ab0","statuscode":1,"creditonhold":false,"donotbulkemail":false,"donotsendmm":false,"createdon":"2016-11-09T08:22:54Z","statecode":0,"opendeals_state":1,"businesstypecode":1,"openrevenue_state":7,"_ownerid_value":"a8c706a9-29fd-4a91-bd8d-8f304cf141b5","donotbulkpostalmail":false,"name":"\\RBrMXmdTJ_juPepgBMbvHxS^s","donotemail":false,"donotphone":false,"donotpostalmail":false,"opendeals_date":"2016-11-16T12:31:53Z","versionnumber":646599,"address2_addresstypecode":1,"_modifiedby_value":"a8c706a9-29fd-4a91-bd8d-8f304cf141b5","shippingmethodcode":1,"followemail":true,"preferredcontactmethodcode":1,"_owningbusinessunit_value":"e5307675-2a9f-e611-80f8-c4346bc50748","_createdby_value":"a8c706a9-29fd-4a91-bd8d-8f304cf141b5","opendeals":0,"donotfax":false,"merged":false,"customersizecode":1,"marketingonly":false,"accountratingcode":1,"address2_addressid":"e93cd4bf-abcc-4198-8dbc-aa10d5f8cd06","_owninguser_value":"a8c706a9-29fd-4a91-bd8d-8f304cf141b5","modifiedon":"2016-11-09T08:32:39Z","participatesinworkflow":false,"accountclassificationcode":1,"address1_addressid":"e12d3c57-905c-4368-92ff-b5d13e3d5bc3","openrevenue":null,"lastusedincampaign":null,"address1_name":null,"address1_telephone2":null,"overriddencreatedon":null,"entityimageid":null,"ownershipcode":null,"creditlimit":null,"_primarycontactid_value":null,"address1_county":null,"entityimage_timestamp":null,"telephone3":null,"address1_freighttermscode":null,"address2_line1":null,"onholdtime":null,"_createdbyexternalparty_value":null,"telephone2":null,"primarysatoriid":null,"address2_telephone1":null,"address1_fax":null,"_createdonbehalfby_value":null,"address2_city":null,"address2_latitude":null,"address2_composite":null,"creditlimit_base":null,"int_facebook":null,"aging90_base":null,"address2_postalcode":null,"address2_name":null,"preferredappointmentdaycode":null,"_owningteam_value":null,"address2_line3":null,"revenue_base":null,"aging30":null,"ftpsiteurl":null,"_originatingleadid_value":null,"emailaddress3":null,"_modifiedbyexternalparty_value":null,"paymenttermscode":null,"websiteurl":null,"revenue":null,"address1_shippingmethodcode":null,"description":null,"tickersymbol":null,"lastonholdtime":null,"marketcap":null,"_parentaccountid_value":null,"_preferredsystemuserid_value":null,"address2_longitude":null,"_defaultpricelevelid_value":null,"address1_primarycontactname":null,"int_twitter":null,"accountcategorycode":null,"marketcap_base":null,"adx_modifiedbyusername":null,"address2_telephone3":null,"address2_postofficebox":null,"_preferredserviceid_value":null,"importsequencenumber":null,"address1_upszone":null,"stageid":null,"utcconversiontimezonecode":null,"aging60":null,"customertypecode":null,"telephone1":null,"address1_postofficebox":null,"_msa_managingpartnerid_value":null,"yominame":null,"address2_country":null,"address1_line2":null,"address1_postalcode":null,"traversedpath":null,"_transactioncurrencyid_value":null,"fax":null,"numberofemployees":null,"address2_upszone":null,"aging90":null,"address1_longitude":null,"emailaddress2":null,"address1_telephone1":null,"adx_modifiedbyipaddress":null,"exchangerate":null,"address1_stateorprovince":null,"address2_line2":null,"entityimage":null,"primarytwitterid":null,"timespentbymeonemailandmeetings":null,"accountnumber":null,"address1_line1":null,"_territoryid_value":null,"address1_composite":null,"_slaid_value":null,"address2_county":null,"_preferredequipmentid_value":null,"sic":null,"adx_createdbyusername":null,"entityimage_url":null,"processid":null,"address2_telephone2":null,"address1_addresstypecode":null,"address1_utcoffset":null,"address2_fax":null,"aging60_base":null,"address1_country":null,"_modifiedonbehalfby_value":null,"stockexchange":null,"address1_line3":null,"adx_createdbyipaddress":null,"aging30_base":null,"openrevenue_base":null,"sharesoutstanding":null,"_slainvokedid_value":null,"address1_city":null,"address2_primarycontactname":null,"address1_latitude":null,"_masterid_value":null,"address2_utcoffset":null,"emailaddress1":null,"timezoneruleversionnumber":null,"address2_stateorprovince":null,"preferredappointmenttimecode":null,"industrycode":null,"address1_telephone3":null
    },{
      "@odata.etag":"W/\"646591\"","openrevenue_date":"2016-11-16T12:31:53Z","territorycode":1,"address2_freighttermscode":1,"address2_shippingmethodcode":1,"accountid":"ca4a8f7a-51a6-e611-80fa-c4346bc562c4","statuscode":1,"creditonhold":false,"donotbulkemail":false,"donotsendmm":false,"createdon":"2016-11-09T07:52:37Z","statecode":0,"opendeals_state":1,"businesstypecode":1,"openrevenue_state":7,"_ownerid_value":"a8c706a9-29fd-4a91-bd8d-8f304cf141b5","donotbulkpostalmail":false,"name":"teWfvgYJZKvgYJu","donotemail":false,"donotphone":false,"donotpostalmail":false,"opendeals_date":"2016-11-16T12:31:53Z","versionnumber":646591,"address2_addresstypecode":1,"_modifiedby_value":"a8c706a9-29fd-4a91-bd8d-8f304cf141b5","shippingmethodcode":1,"followemail":true,"preferredcontactmethodcode":1,"_owningbusinessunit_value":"e5307675-2a9f-e611-80f8-c4346bc50748","_createdby_value":"a8c706a9-29fd-4a91-bd8d-8f304cf141b5","opendeals":0,"donotfax":false,"merged":false,"customersizecode":1,"marketingonly":false,"accountratingcode":1,"address2_addressid":"32184ac3-b065-46e7-a831-937c3494521f","_owninguser_value":"a8c706a9-29fd-4a91-bd8d-8f304cf141b5","modifiedon":"2016-11-09T08:32:39Z","participatesinworkflow":false,"accountclassificationcode":1,"address1_addressid":"34610224-62d5-42d9-ac70-51c498547a76","openrevenue":null,"lastusedincampaign":null,"address1_name":null,"address1_telephone2":null,"overriddencreatedon":null,"entityimageid":null,"ownershipcode":null,"creditlimit":null,"_primarycontactid_value":null,"address1_county":null,"entityimage_timestamp":null,"telephone3":null,"address1_freighttermscode":null,"address2_line1":null,"onholdtime":null,"_createdbyexternalparty_value":null,"telephone2":null,"primarysatoriid":null,"address2_telephone1":null,"address1_fax":null,"_createdonbehalfby_value":null,"address2_city":null,"address2_latitude":null,"address2_composite":null,"creditlimit_base":null,"int_facebook":null,"aging90_base":null,"address2_postalcode":null,"address2_name":null,"preferredappointmentdaycode":null,"_owningteam_value":null,"address2_line3":null,"revenue_base":null,"aging30":null,"ftpsiteurl":null,"_originatingleadid_value":null,"emailaddress3":null,"_modifiedbyexternalparty_value":null,"paymenttermscode":null,"websiteurl":null,"revenue":null,"address1_shippingmethodcode":null,"description":null,"tickersymbol":null,"lastonholdtime":null,"marketcap":null,"_parentaccountid_value":null,"_preferredsystemuserid_value":null,"address2_longitude":null,"_defaultpricelevelid_value":null,"address1_primarycontactname":null,"int_twitter":null,"accountcategorycode":null,"marketcap_base":null,"adx_modifiedbyusername":null,"address2_telephone3":null,"address2_postofficebox":null,"_preferredserviceid_value":null,"importsequencenumber":null,"address1_upszone":null,"stageid":null,"utcconversiontimezonecode":null,"aging60":null,"customertypecode":null,"telephone1":null,"address1_postofficebox":null,"_msa_managingpartnerid_value":null,"yominame":null,"address2_country":null,"address1_line2":null,"address1_postalcode":null,"traversedpath":null,"_transactioncurrencyid_value":null,"fax":null,"numberofemployees":null,"address2_upszone":null,"aging90":null,"address1_longitude":null,"emailaddress2":null,"address1_telephone1":null,"adx_modifiedbyipaddress":null,"exchangerate":null,"address1_stateorprovince":null,"address2_line2":null,"entityimage":null,"primarytwitterid":null,"timespentbymeonemailandmeetings":null,"accountnumber":null,"address1_line1":null,"_territoryid_value":null,"address1_composite":null,"_slaid_value":null,"address2_county":null,"_preferredequipmentid_value":null,"sic":null,"adx_createdbyusername":null,"entityimage_url":null,"processid":null,"address2_telephone2":null,"address1_addresstypecode":null,"address1_utcoffset":null,"address2_fax":null,"aging60_base":null,"address1_country":null,"_modifiedonbehalfby_value":null,"stockexchange":null,"address1_line3":null,"adx_createdbyipaddress":null,"aging30_base":null,"openrevenue_base":null,"sharesoutstanding":null,"_slainvokedid_value":null,"address1_city":null,"address2_primarycontactname":null,"address1_latitude":null,"_masterid_value":null,"address2_utcoffset":null,"emailaddress1":null,"timezoneruleversionnumber":null,"address2_stateorprovince":null,"preferredappointmenttimecode":null,"industrycode":null,"address1_telephone3":null
    },{
      "@odata.etag":"W/\"646593\"","openrevenue_date":"2016-11-16T12:31:53Z","territorycode":1,"address2_freighttermscode":1,"address2_shippingmethodcode":1,"accountid":"d69eb9b1-52a6-e611-80fa-c4346bc562c4","statuscode":1,"creditonhold":false,"donotbulkemail":false,"donotsendmm":false,"createdon":"2016-11-09T08:01:19Z","statecode":0,"opendeals_state":1,"businesstypecode":1,"openrevenue_state":7,"_ownerid_value":"a8c706a9-29fd-4a91-bd8d-8f304cf141b5","donotbulkpostalmail":false,"name":"LwhZKvgYKugXeW","donotemail":false,"donotphone":false,"donotpostalmail":false,"opendeals_date":"2016-11-16T12:31:53Z","versionnumber":646593,"address2_addresstypecode":1,"_modifiedby_value":"a8c706a9-29fd-4a91-bd8d-8f304cf141b5","shippingmethodcode":1,"followemail":true,"preferredcontactmethodcode":1,"_owningbusinessunit_value":"e5307675-2a9f-e611-80f8-c4346bc50748","_createdby_value":"a8c706a9-29fd-4a91-bd8d-8f304cf141b5","opendeals":0,"donotfax":false,"merged":false,"customersizecode":1,"marketingonly":false,"accountratingcode":1,"address2_addressid":"135e6464-a04a-4e2f-8ee7-de1088f6b2fc","_owninguser_value":"a8c706a9-29fd-4a91-bd8d-8f304cf141b5","modifiedon":"2016-11-09T08:32:39Z","participatesinworkflow":false,"accountclassificationcode":1,"address1_addressid":"2ec27dc6-e50b-4043-b1fe-126f7baf2150","openrevenue":null,"lastusedincampaign":null,"address1_name":null,"address1_telephone2":null,"overriddencreatedon":null,"entityimageid":null,"ownershipcode":null,"creditlimit":null,"_primarycontactid_value":null,"address1_county":null,"entityimage_timestamp":null,"telephone3":null,"address1_freighttermscode":null,"address2_line1":null,"onholdtime":null,"_createdbyexternalparty_value":null,"telephone2":null,"primarysatoriid":null,"address2_telephone1":null,"address1_fax":null,"_createdonbehalfby_value":null,"address2_city":null,"address2_latitude":null,"address2_composite":null,"creditlimit_base":null,"int_facebook":null,"aging90_base":null,"address2_postalcode":null,"address2_name":null,"preferredappointmentdaycode":null,"_owningteam_value":null,"address2_line3":null,"revenue_base":null,"aging30":null,"ftpsiteurl":null,"_originatingleadid_value":null,"emailaddress3":null,"_modifiedbyexternalparty_value":null,"paymenttermscode":null,"websiteurl":null,"revenue":null,"address1_shippingmethodcode":null,"description":null,"tickersymbol":null,"lastonholdtime":null,"marketcap":null,"_parentaccountid_value":null,"_preferredsystemuserid_value":null,"address2_longitude":null,"_defaultpricelevelid_value":null,"address1_primarycontactname":null,"int_twitter":null,"accountcategorycode":null,"marketcap_base":null,"adx_modifiedbyusername":null,"address2_telephone3":null,"address2_postofficebox":null,"_preferredserviceid_value":null,"importsequencenumber":null,"address1_upszone":null,"stageid":null,"utcconversiontimezonecode":null,"aging60":null,"customertypecode":null,"telephone1":null,"address1_postofficebox":null,"_msa_managingpartnerid_value":null,"yominame":null,"address2_country":null,"address1_line2":null,"address1_postalcode":null,"traversedpath":null,"_transactioncurrencyid_value":null,"fax":null,"numberofemployees":null,"addr



使用这种方式批量执行脚本的缺陷是我们需要自己去解析返回的报文,这可能需要我们花费不少功夫。

以上是关于Dynamics CRM 2015/2016/365 Web API:批处理任务的主要内容,如果未能解决你的问题,请参考以下文章

关于MS Dynamics AX 和 MS Dynamics CRM实施

css CRM控制样式#CSS #MoussaElAnnan#CRM2016#Dynamics365

Dynamics CRM 2016 VS Dynamics 365 – 启用数据加密功能报错

无网络环境下安装Dynamics CRM

Dynamics CRM 2016 Web API 消息列表

Dynamics CRM 资源