Friday, August 28, 2009

How to convert a RetrieveMultiple query written Server side to SOAP message to be used in Javascripts

It has often been seen that a RetrieveMultiple query that is very easy to write using the server side Query Expression objects using CRM webservices becomes difficult to achieve if we need to create a SOAP message for the same.

In this article we hope to explain each of the elements involved in the writing of SOAP messages to be used through Javascripts.

Let us take a simple RetrieveMultiple code as an example.

While writing the SOAP message the first component required is the SOAP envelope. This is the same at all times for all SOAP messages.

"<?xml version='1.0' encoding='utf-8'?>"+
"<soap:Envelope xmlns:soap=''"+
" xmlns:xsi=''"+
" xmlns:xsd=''>"+

Next we need to add the Authentication Header that needs to be passed along with the SOAP message for successful authentication with the CRM Web service. CRM has an inbuilt function GenerateAuthenticationHeader() that can be used for this purpose.

var authenticationHeader = GenerateAuthenticationHeader();

SOAP Body is where the actual SDK message to be executed is included.


In our example we are using the RetrieveMultiple Message so we provide the Message name and the schema reference to the CRM webservices

"<RetrieveMultiple xmlns=''>"+

The parameter to a Retrieve Multiple Message is always a query expression

QueryExpression query = new QueryExpression();

"<query xmlns:q1=''"+
" xsi:type='q1:QueryExpression'>"+

Now include all properties of the Query object that you set.

Entity name

query.EntityName = EntityName.incident.ToString();


Column Set

query.ColumnSet = new AllColumns();

"<q1:ColumnSet xsi:type='q1:ColumnSet'>"+


ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "title";
condition.Operator = ConditionOperator.Like;
condition.Values = new string [] {strSearchText};

"<q1:Value xsi:type='xsd:string'>"+searchtext +"</q1:Value>"+

Here, we have used 'Like' condition operator, likewise one can use any of the sdk supported operators and change the values to be matched accordingly.

Order by

OrderExpression oe = new OrderExpression();
oe.AttributeName = "title";
oe.OrderType = OrderType.Ascending;
query.Orders = new OrderExpression[]{oe};

"<q1:Orders>" +
"<q1:Order>" +
"<q1:AttributeName>title</q1:AttributeName>" +
"<q1:OrderType>Ascending</q1:OrderType>" +
"</q1:Order>" +
"</q1:Orders>" +

Once you have set all of these… your SOAP message is ready for execution.

No comments:

Post a Comment