POWEROBJECTS

718 Washington Ave. N. Suite #101
Minneapolis, MN 55401
View map and all Locations

Send us a message

JoeCRM says, “Microsoft Dynamics CRM SOAP Logger makes me happy!”

crm2011

Writing a fetch can be time consuming and aggravating, even if you know what you’re doing.  The common niceties that developers have come to enjoy, like IntelliSense for instance, don’t exist.  Writing queries becomes trial and error and can greatly increase the time it takes to finish your project.

Fortunately, the CRM 2011 SDK comes with an application that allows you to write your queries in Visual Studio and capture the format of the fetch.  This application, SOAP Logger, lives in a less than obvious location:

crm2011sdkrootsamplecodecsclientsoaplogger

Note that there are three regions in the code:

Microsoft Dynamics CRM SOAP logger

(If you don’t see this view, you can use Ctrl+M Ctrl+O to contract your code)

The only area we’ll need to modify is the region CodeToCapture.  Here you’ll be able to write a query expression and the application will log the request.  For now, let’s focus on the query and we’ll come back to what is captured later.

Inside the CodeToCapture region, let’s do a simple query to retrieve all Contacts whose last name is “Smith”.  In order to do this, we’d write a query like the following:

[sourcecode language=”js”]var query = new QueryExpression("contact");
query.Criteria.AddCondition("lastname", ConditionOperator.Equal, "Smith");
[/sourcecode]

For context, this is where the query is placed in the method:

[sourcecode language=”js”]
/// <summary>
/// This method connects to the Organization service.
/// </summary>
/// <param name="serverConfig">Contains server connection information.</param>
//<snippetSOAPLogger1>
public void Run(ServerConnection.Configuration serverConfig)
{
try
{

// Connect to the Organization service.
// The using statement assures that the service proxy will be properly disposed.
using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
{
// This statement is required to enable early-bound type support.
_serviceProxy.EnableProxyTypes();

IOrganizationService service = (IOrganizationService)_serviceProxy;

using (StreamWriter output = new StreamWriter("output.txt"))
{

SoapLoggerOrganizationService slos = new SoapLoggerOrganizationService(serverConfig.OrganizationUri, service, output);

//Add the code you want to test here:
// You must use the SoapLoggerOrganizationService ‘slos’ proxy rather than the IOrganizationService proxy you would normally use.
var query = new QueryExpression("contact");
query.Criteria.AddCondition("lastname", ConditionOperator.Equal, "Smith");
slos.RetrieveMultiple(query);

}

}

}

// Catch any service fault exceptions that Microsoft Dynamics CRM throws.
catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
{
// You can handle an exception here or pass it back to the calling method.
throw;
}
}
//</snippetSOAPLogger1>

[/sourcecode]

Now, we just run the application from Visual Studio.  A large amount of data has been redacted here, but the values you provide should be somewhat obvious for you.

SOAPLogger

After choosing your organization, the program will end and you can press the Enter key to exit.

SOAPLogger

Navigate to the folder

crm2011sdkrootsamplecodecsclientsoaploggersoaploggerbinDebug

You should see a file called output.txt, the content of which should include the HTTP Request and Response.  The request for our query looks like the following:

[sourcecode language=”plain”]
HTTP REQUEST
————————————————–
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<RetrieveMultiple xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<query i:type="a:QueryExpression" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
<a:ColumnSet>
<a:AllColumns>false</a:AllColumns>
<a:Columns xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
</a:ColumnSet>
<a:Criteria>
<a:Conditions>
<a:ConditionExpression>
<a:AttributeName>lastname</a:AttributeName>
<a:Operator>Equal</a:Operator>
<a:Values xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<b:anyType i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">Smith</b:anyType>
</a:Values>
</a:ConditionExpression>
</a:Conditions>
<a:FilterOperator>And</a:FilterOperator>
<a:Filters />
</a:Criteria>
<a:Distinct>false</a:Distinct>
<a:EntityName>contact</a:EntityName>
<a:LinkEntities />
<a:Orders />
<a:PageInfo>
<a:Count>0</a:Count>
<a:PageNumber>0</a:PageNumber>
<a:PagingCookie i:nil="true" />
<a:ReturnTotalRecordCount>false</a:ReturnTotalRecordCount>
</a:PageInfo>
<a:NoLock>false</a:NoLock>
</query>
</RetrieveMultiple>
</s:Body>
</s:Envelope>
————————————————–
[/sourcecode]

With the aid of an XMLHttpWebRequest, you can now utilize this XML to get the information you want from CRM.

Psssst….If this post makes your head spin, reach out to us…we can help. PowerObjects provides Microsoft Dynamics CRM Service, Support, Education, and Add-Ons.

Happy CRM’ing!

Avatar for JoeCRM

JoeCRM

Joe CRM is a CRM superhero who runs on pure Microsoft Dynamics CRM adrenaline. As the face of PowerObjects, Joe CRM’s mission is to reveal innovative ways to use Dynamics CRM and bring the application to more businesses and organizations around the world.