本文来探索下在弱客户端下通过FetchExpression来访问Dynamics CRM 2011系统中的数据。在Dynamics CRM 2011系统中客户端居然用FetchExpression来获取系统中的数据,看上去是那么的复古(当前开发人员可以使用系统的REST端点)。但本文仅仅起到一个抛砖引玉的作用,Dynamics CRM 2011 系统存在大量的消息类(API)。通过这些消息类,我们可以完成对系统的各种操作(禁用记录,激活工作流等),详细内容各位看官可以参阅文章《Dynamics CRM 2011 消息类列表》。
当然啦,如果我们在强客户端(C#,VB.Net)调用这些消息类。微软会为我们提供非常好的开发人员体验,SOAP报文的发送和接收完全被封装,一切都不需要自己干预。但如果我们要在弱客户端上调用这些消息类,那该如何是好呢?元芳你怎么看呢?
各位看官莫慌,莫慌,请参考文章《Dynamics CRM 2011编程系列(50):使用SOAPLogger抓取报文》,在这里我已经给出了解决方案了。
好吧,现在一切准备都完毕了,让我们动起来吧。
FetchExpression fetchExp = new FetchExpression(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='contact'> <attribute name='fullname' /> <attribute name='contactid' /> <order attribute='fullname' descending='false' /> </entity> </fetch>"); slos.RetrieveMultiple(fetchExp);
HTTP REQUEST -------------------------------------------------- POST http://msstore-test-02.northamerica.corp.microsoft.com:5550/Example/XRMServices/2011/Organization.svc/web Content-Type: text/xml; charset=utf-8 SOAPAction: http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/RetrieveMultiple <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <RetrieveMultiple xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <query i:type="a:FetchExpression" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts"> <a:Query><fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='contact'> <attribute name='fullname' /> <attribute name='contactid' /> <order attribute='fullname' descending='false' /> </entity> </fetch></a:Query> </query> </RetrieveMultiple> </s:Body> </s:Envelope> --------------------------------------------------
图1
var queryurl = "http://Server Name/Organization Name/XRMServices/2011/Organization.svc/web"; var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); var fetchExp = ['<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">' , '<s:Body>' , '<RetrieveMultiple xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">' , '<query i:type="a:FetchExpression" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">' , '<a:Query>' , '<![CDATA[' , '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">' , ' <entity name="contact">' , ' <attribute name="fullname" />' , ' <attribute name="contactid" />' , ' <order attribute="fullname" descending="false" />' , ' </entity>' , '</fetch>' , ']]>' , '</a:Query>' , '</query>' , '</RetrieveMultiple>' , '</s:Body>' , '</s:Envelope>']; xmlhttp.Open("POST", queryurl, false); xmlhttp.setRequestHeader("Accept", "application/xml, text/xml, */*"); xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xmlhttp.setRequestHeader("SOAPAction", " http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/RetrieveMultiple"); xmlhttp.Send(fetchExp.join('').toString()); if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { alert(xmlhttp.responseText); } } else { }
图2
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <RetrieveMultipleResponse xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <RetrieveMultipleResult xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts"> <a:Entities> <a:Entity> <a:Attributes xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic"> <a:KeyValuePairOfstringanyType> <b:key>fullname</b:key> <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">Bob Li</b:value> </a:KeyValuePairOfstringanyType> <a:KeyValuePairOfstringanyType> <b:key>contactid</b:key> <b:value i:type="c:guid" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/">441f1166-5c56-e211-af85-00155d1ce818</b:value> </a:KeyValuePairOfstringanyType> </a:Attributes> <a:EntityState i:nil="true" /> <a:FormattedValues xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" /> <a:Id>441f1166-5c56-e211-af85-00155d1ce818</a:Id> <a:LogicalName>contact</a:LogicalName> <a:RelatedEntities xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" /> </a:Entity> <a:Entity> <a:Attributes xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic"> <a:KeyValuePairOfstringanyType> <b:key>fullname</b:key> <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">Clark Wang1</b:value> </a:KeyValuePairOfstringanyType> <a:KeyValuePairOfstringanyType> <b:key>contactid</b:key> <b:value i:type="c:guid" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/">71e70aab-5c56-e211-af85-00155d1ce818</b:value> </a:KeyValuePairOfstringanyType> </a:Attributes> <a:EntityState i:nil="true" /> <a:FormattedValues xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" /> <a:Id>71e70aab-5c56-e211-af85-00155d1ce818</a:Id> <a:LogicalName>contact</a:LogicalName> <a:RelatedEntities xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" /> </a:Entity> <a:Entity> <a:Attributes xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic"> <a:KeyValuePairOfstringanyType> <b:key>fullname</b:key> <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">Hitesh White</b:value> </a:KeyValuePairOfstringanyType> <a:KeyValuePairOfstringanyType> <b:key>contactid</b:key> <b:value i:type="c:guid" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/">87cf252c-7463-e211-9a59-00155d1ce818</b:value> </a:KeyValuePairOfstringanyType> </a:Attributes> <a:EntityState i:nil="true" /> <a:FormattedValues xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" /> <a:Id>87cf252c-7463-e211-9a59-00155d1ce818</a:Id> <a:LogicalName>contact</a:LogicalName> <a:RelatedEntities xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" /> </a:Entity> <a:Entity> <a:Attributes xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic"> <a:KeyValuePairOfstringanyType> <b:key>fullname</b:key> <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">Jim Green</b:value> </a:KeyValuePairOfstringanyType> <a:KeyValuePairOfstringanyType> <b:key>contactid</b:key> <b:value i:type="c:guid" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/">396a665a-5c56-e211-af85-00155d1ce818</b:value> </a:KeyValuePairOfstringanyType> </a:Attributes> <a:EntityState i:nil="true" /> <a:FormattedValues xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" /> <a:Id>396a665a-5c56-e211-af85-00155d1ce818</a:Id> <a:LogicalName>contact</a:LogicalName> <a:RelatedEntities xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" /> </a:Entity> </a:Entities> <a:EntityName>contact</a:EntityName> <a:MinActiveRowVersion>-1</a:MinActiveRowVersion> <a:MoreRecords>false</a:MoreRecords> <a:PagingCookie><cookie page="1"><fullname last="Jim Green" first="Bob Li" /><contactid last="{396A665A-5C56-E211-AF85-00155D1CE818}" first="{441F1166-5C56-E211-AF85-00155D1CE818}" /></cookie></a:PagingCookie> <a:TotalRecordCount>-1</a:TotalRecordCount> <a:TotalRecordCountLimitExceeded>false</a:TotalRecordCountLimitExceeded> </RetrieveMultipleResult> </RetrieveMultipleResponse> </s:Body> </s:Envelope>