2012-06-20 59 views
4

我們試圖使用SOAP根據N:N關係檢索相關記錄。所以我們從當前表單中檢索到1個實體的GUID。然後我們需要來自其他實體的所有GUID。我們如何正確地形成我們的SOAP請求。如何根據N:N關係檢索SOAP中的相關記錄

RetrieveContacts = function (EntityName, EntityKeyName, contactGUID) { 
    // Prepare IDs to retrieve the records 
    var authenticationHeader = GenerateAuthenticationHeader(); 

    // Prepare the SOAP message. 
    var xml = ""; 
    xml += "<?xml version='1.0' encoding='utf-8'?>"; 
    xml += "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"; 
    xml += " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"; 
    xml += " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"; 
    xml += authenticationHeader; 
    xml += "<soap:Body>"; 
    xml += "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"; 
    xml += "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'"; 
    xml += " xsi:type='q1:QueryExpression'>"; 
    xml += "<q1:EntityName>" + EntityName + "</q1:EntityName>"; 
    xml += "<q1:ColumnSet xsi:type='q1:ColumnSet'>"; 
    xml += "<q1:Attributes>"; 
    xml += "<q1:Attribute>" + EntityKeyName + "</q1:Attribute>"; 
    xml += "</q1:Attributes>"; 
    xml += "</q1:ColumnSet>"; 
    xml += "<q1:Distinct>false</q1:Distinct>"; 
    xml += "<q1:Criteria>"; 
    xml += "<q1:Conditions>"; 
    xml += "<q1:Condition>"; 
    xml += "<q1:AttributeName>" + EntityKeyName + "</q1:AttributeName>"; 
    xml += "<q1:Operator>Equal</q1:Operator>"; 
    xml += "<q1:Values>"; 
    xml += "<q1:Value xsi:type='xsd:string'>" + contactGUID + "</q1:Value>"; 
    xml += "</q1:Values>"; 
    xml += "</q1:Condition>"; 
    xml += "</q1:Conditions>"; 
    xml += "</q1:Criteria>"; 
    xml += "</query>"; 
    xml += "</RetrieveMultiple>"; 
    xml += "</soap:Body>"; 
    xml += "</soap:Envelope>"; 
    // Prepare the xmlHttpObject and send the request. 
    var xHReq = new ActiveXObject("Msxml2.XMLHTTP"); 
    xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); 
    xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple"); 
    xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 
    xHReq.setRequestHeader("Content-Length", xml.length); 
    xHReq.send(xml); 
    // Capture the result. 
    var resultXml = xHReq.responseXML; 

    // Check for errors. 
    var errorCount = resultXml.selectNodes('//error').length; 
    if (errorCount != 0) { 
     var msg = resultXml.selectSingleNode('//description').nodeTypedValue; 
     alert(msg); 
    } 
    // Parse and display the results. 
    else { 
     var results = resultXml.getElementsByTagName('BusinessEntity'); 
     var msg = ""; 
     if (results.length == 0) { 
      msg = "No records to submit."; 
      alert(msg); 
      return; 
     } 
     else { 
      for (i = 0; i < results.length; i++) { 
       var idValue = results[i].selectSingleNode('./q1:' + EntityKeyName).nodeTypedValue; 
       //Now insert the new RSVP record 
       msg += idValue + "\t" + name + "\r"; 
      } 
      alert(msg); 
     } 
    } 
} 

回答