CRM4.0多个实体关联查询

C#代码实现如下

  LinkEntity link = new LinkEntity();

            link.LinkFromEntityName = "new_ddxy";

            link.LinkFromAttributeName = "new_salesorder";

            link.LinkToEntityName = "salesorder";

            link.LinkToAttributeName = "salesorderid";

            ConditionExpression cond = new ConditionExpression();

            cond.AttributeName = "ordernumber";

            cond.Operator = ConditionOperator.Equal;

            cond.Values = new string[] { "ORD-04412-708MJ0" };

            FilterExpression filter = new FilterExpression();

            filter.Conditions = new ConditionExpression[] { cond };

            filter.FilterOperator = LogicalOperator.And;

            link.JoinOperator = JoinOperator.Inner;

            link.LinkCriteria = filter;



            LinkEntity link2 = new LinkEntity();

            link2.LinkFromEntityName = "new_ddxy";

            link2.LinkFromAttributeName = "new_productpricelevel_kz";

            link2.LinkToEntityName = "new_productpricelevel_kz";

            link2.LinkToAttributeName = "new_productpricelevel_kzid";

            ConditionExpression cond2 = new ConditionExpression();

            cond2.AttributeName = "new_sftb";

            cond2.Operator = ConditionOperator.Equal;

            cond2.Values = new object[] { true };

            link2.JoinOperator = JoinOperator.Inner;



            FilterExpression filter2 = new FilterExpression();

            filter2.FilterOperator = LogicalOperator.And;

            filter2.Conditions = new ConditionExpression[] { cond2 };

            link2.LinkCriteria = filter2;



            QueryExpression query = new QueryExpression();

            query.EntityName = "new_ddxy";

            query.ColumnSet = new AllColumns();

            query.LinkEntities = new LinkEntity[] { link, link2 };

            BusinessEntityCollection entities = (BusinessEntityCollection)service.RetrieveMultiple(query);



            

//********************开始查询*****************

var xml = "" + 

"<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 

"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" + 

"  <soap:Header>" + 

"    <CrmAuthenticationToken xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 

"      <AuthenticationType xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">0</AuthenticationType>" + 

"      <OrganizationName xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">SZYQ</OrganizationName>" + 

"      <CallerId xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">00000000-0000-0000-0000-000000000000</CallerId>" + 

"    </CrmAuthenticationToken>" + 

"  </soap:Header>" + 

"  <soap:Body>" + 

"    <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 

"      <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" + 

"        <q1:EntityName>new_ddxy</q1:EntityName>" + 

"        <q1:ColumnSet xsi:type=\"q1:AllColumns\" />" + 

"        <q1:Distinct>false</q1:Distinct>" + 

"        <q1:LinkEntities>" + 

"          <q1:LinkEntity>" + 

"            <q1:LinkFromAttributeName>new_salesorder</q1:LinkFromAttributeName>" + 

"            <q1:LinkFromEntityName>new_ddxy</q1:LinkFromEntityName>" + 

"            <q1:LinkToEntityName>salesorder</q1:LinkToEntityName>" + 

"            <q1:LinkToAttributeName>salesorderid</q1:LinkToAttributeName>" + 

"            <q1:JoinOperator>Inner</q1:JoinOperator>" + 

"            <q1:LinkCriteria>" + 

"              <q1:FilterOperator>And</q1:FilterOperator>" + 

"              <q1:Conditions>" + 

"                <q1:Condition>" + 

"                  <q1:AttributeName>ordernumber</q1:AttributeName>" + 

"                  <q1:Operator>Equal</q1:Operator>" + 

"                  <q1:Values>" + 

"                    <q1:Value xsi:type=\"xsd:string\">ORD-04412-708MJ0</q1:Value>" + 

"                  </q1:Values>" + 

"                </q1:Condition>" + 

"              </q1:Conditions>" + 

"            </q1:LinkCriteria>" + 

"          </q1:LinkEntity>" + 

"          <q1:LinkEntity>" + 

"            <q1:LinkFromAttributeName>new_productpricelevel_kz</q1:LinkFromAttributeName>" + 

"            <q1:LinkFromEntityName>new_ddxy</q1:LinkFromEntityName>" + 

"            <q1:LinkToEntityName>new_productpricelevel_kz</q1:LinkToEntityName>" + 

"            <q1:LinkToAttributeName>new_productpricelevel_kzid</q1:LinkToAttributeName>" + 

"            <q1:JoinOperator>Inner</q1:JoinOperator>" + 

"            <q1:LinkCriteria>" + 

"              <q1:FilterOperator>And</q1:FilterOperator>" + 

"              <q1:Conditions>" + 

"                <q1:Condition>" + 

"                  <q1:AttributeName>new_sftb</q1:AttributeName>" + 

"                  <q1:Operator>Equal</q1:Operator>" + 

"                  <q1:Values>" + 

"                    <q1:Value xsi:type=\"xsd:boolean\">true</q1:Value>" + 

"                  </q1:Values>" + 

"                </q1:Condition>" + 

"              </q1:Conditions>" + 

"            </q1:LinkCriteria>" + 

"          </q1:LinkEntity>" + 

"        </q1:LinkEntities>" + 

"      </query>" + 

"    </RetrieveMultiple>" + 

"  </soap:Body>" + 

"</soap:Envelope>" + 

"";



var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");



xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");

xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

xmlHttpRequest.send(xml);



var resultXml = xmlHttpRequest.responseXML;

alert(resultXml.xml);

//*********************返回结果******************



<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <soap:Body>

    <RetrieveMultipleResponse xmlns="http://schemas.microsoft.com/crm/2007/WebServices">

      <RetrieveMultipleResult EntityName="new_ddxy" MoreRecords="0" PagingCookie="">

        <BusinessEntities xmlns="http://schemas.microsoft.com/crm/2006/WebServices">

          <BusinessEntity xmlns:q1="http://schemas.microsoft.com/crm/2007/WebServices" xsi:type="q1:new_ddxy">

            <q1:statecode formattedvalue="可用">Active</q1:statecode>

            <q1:new_cklsxx name="否">0</q1:new_cklsxx>

            <q1:new_productpricelevel_kz name="网上申报(一年)" dsc="0">{200754BD-2390-DF11-8C10-000C29C44AE5}</q1:new_productpricelevel_kz>

            <q1:createdon date="2010-7-27" time="14:56">2010-07-27T14:56:59+08:00</q1:createdon>

            <q1:new_yxbz name="否">0</q1:new_yxbz>

            <q1:new_tbyxq name="否">0</q1:new_tbyxq>

            <q1:new_yxqz date="2010-7-13" time="0:00">2010-07-13T00:00:00+08:00</q1:new_yxqz>

            <q1:new_ddxyid>{CCC8A526-4C99-DF11-9F44-000C29C44AE5}</q1:new_ddxyid>

            <q1:new_yxqq date="2010-7-21" time="0:00">2010-07-21T00:00:00+08:00</q1:new_yxqq>

            <q1:ownerid name="CRM系统定制员" dsc="0" type="systemuser">{35F91B00-7B64-DF11-BEB9-000C29692715}</q1:ownerid>

            <q1:modifiedon date="2010-7-27" time="16:35">2010-07-27T16:35:59+08:00</q1:modifiedon>

            <q1:statuscode name="可用">1</q1:statuscode>

            <q1:new_name>2101140000001866264L</q1:new_name>

            <q1:timezoneruleversionnumber formattedvalue="0">0</q1:timezoneruleversionnumber>

            <q1:modifiedby dsc="0" name="SYSTEM">{AD97F2C4-DDD5-41B2-8D59-359ED8DE1598}</q1:modifiedby>

            <q1:owningbusinessunit>{A02CA4E9-315B-DF11-9A5A-000C29692715}</q1:owningbusinessunit>

            <q1:new_fwbh>90000000000001265744</q1:new_fwbh>

            <q1:new_sfqetk name="否">0</q1:new_sfqetk>

            <q1:createdby name="SYSTEM" dsc="0">{AD97F2C4-DDD5-41B2-8D59-359ED8DE1598}</q1:createdby>

            <q1:new_salesorder name="陈刚" dsc="0">{8C9C6320-4C99-DF11-9F44-000C29C44AE5}</q1:new_salesorder>

            <q1:new_sftb name="否">0</q1:new_sftb>

          </BusinessEntity>

        </BusinessEntities>

      </RetrieveMultipleResult>

    </RetrieveMultipleResponse>

  </soap:Body>

</soap:Envelope>

你可能感兴趣的:(关联查询)