MSCrm javascript queryByAttribute
我自已实现的
function getq1AttributesString(attributes)
{
var strRet="";
for(i=0;i<attributes.length;i++)
{
strRet+=("<q1:Attribute>"+attributes[i]+"</q1:Attribute>") ;
}
return strRet;
}
function getq1AttributesValue(attributes)
{
var strRet="";
for(i=0;i<attributes.length;i++)
{
strRet+=("<q1:Value xsi:type=\"xsd:string\">"+attributes[i]+"</q1:Value>");
}
return strRet;
}
function queryByAttribute(entityName, columnsetAttributes, queryAttributes, queryAttributesValue)
{
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\">" +GenerateAuthenticationHeader() +
" <soap:Body>" +
" <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryByAttribute\">" +
" <q1:EntityName>"+entityName+"</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" + getq1AttributesString(columnsetAttributes)+
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Attributes>" + getq1AttributesString(queryAttributes)+
" </q1:Attributes>" +
" <q1:Values>" + getq1AttributesValue(queryAttributesValue) +
" </q1:Values>" +
" </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;
return resultXml;
}
var queryAttributes=new Array("pricelevelid","productid","uomid");
var columnsetAttributes=new Array("amount");
var queryAttributesValue=new Array("02BC5E52-021D-DE11-9A15-000AE42C41EE","75E1AB61-27DE-DD11-9A12-000AE42C41EE","A1278C3F-BBDB-DD11-AB5E-002215572EEF");
queryByAttribute("productpricelevel",columnsetAttributes,queryAttributes,queryAttributesValue);
{
var strRet="";
for(i=0;i<attributes.length;i++)
{
strRet+=("<q1:Attribute>"+attributes[i]+"</q1:Attribute>") ;
}
return strRet;
}
function getq1AttributesValue(attributes)
{
var strRet="";
for(i=0;i<attributes.length;i++)
{
strRet+=("<q1:Value xsi:type=\"xsd:string\">"+attributes[i]+"</q1:Value>");
}
return strRet;
}
function queryByAttribute(entityName, columnsetAttributes, queryAttributes, queryAttributesValue)
{
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\">" +GenerateAuthenticationHeader() +
" <soap:Body>" +
" <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryByAttribute\">" +
" <q1:EntityName>"+entityName+"</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" + getq1AttributesString(columnsetAttributes)+
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Attributes>" + getq1AttributesString(queryAttributes)+
" </q1:Attributes>" +
" <q1:Values>" + getq1AttributesValue(queryAttributesValue) +
" </q1:Values>" +
" </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;
return resultXml;
}
var queryAttributes=new Array("pricelevelid","productid","uomid");
var columnsetAttributes=new Array("amount");
var queryAttributesValue=new Array("02BC5E52-021D-DE11-9A15-000AE42C41EE","75E1AB61-27DE-DD11-9A12-000AE42C41EE","A1278C3F-BBDB-DD11-AB5E-002215572EEF");
queryByAttribute("productpricelevel",columnsetAttributes,queryAttributes,queryAttributesValue);