Showing posts with label RetrieveMultiple. Show all posts
Showing posts with label RetrieveMultiple. Show all posts

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='http://schemas.xmlsoap.org/soap/envelope/'"+
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+

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.

"<soap:Body>"+

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='http://schemas.microsoft.com/crm/2007/WebServices'>"+

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

QueryExpression query = new QueryExpression();

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


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

Entity name

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

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

Column Set

query.ColumnSet = new AllColumns();

"<q1:ColumnSet xsi:type='q1:ColumnSet'>"+
"<q1:Attributes>"+
"<q1:Attribute>title</q1:Attribute>"+
"<q1:Attribute>incidentid</q1:Attribute>"+
"</q1:Attributes>"+
"</q1:ColumnSet>"+

Criteria

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

"<q1:Criteria>"+
"<q1:FilterOperator>And</q1:FilterOperator>"+
"<q1:Conditions>"+
"<q1:Condition>"+
"<q1:AttributeName>title</q1:AttributeName>"+
"<q1:Operator>Like</q1:Operator>"+
"<q1:Values>"+
"<q1:Value xsi:type='xsd:string'>"+searchtext +"</q1:Value>"+
"</q1:Values>"+
"</q1:Condition>"+
"</q1:Conditions>"+
"</q1:Criteria>"+

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.