RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Using Microsoft Dynamics CRM 4.0 as a Development Platform : Page 2

Microsoft Dynamics CRM 4.0 provides a robust development platform for server-side developers. Find out how web services exposed by the MS CRM SDK can be used to build line-of-business custom solutions on CRM data.


Building a CRM Helper Class

In this section you will see how to construct a CRMHelper class that acts as a wrapper for the CrmService.

Step 1. Add a Service Reference.

In your web application project (named CRMWebParts in this example), right-click on References and click "Add Web Reference." In the Add Web Reference dialog, search for web services on your local machine. Find the CrmService instance, and click on the service link (see Figure 1). In the web reference name field, specify the name of the service as CrmSdk.

Figure 1. Add Web Reference Dialog: Displays the list of web services available in the local system. Locate the CrmService and add the reference to your project.
Author's Note: There are two versions of the CrmService; one is a backwardly compatible version. Make sure you reference the right version. The URL for the Microsoft Dynamics CRM 4.0 version looks something like http://<servername[:port]>/mscrmservices/2007/crmservice.asmx.

Step 2. Create a CRM Service Instance

You need to create an instance of the CRMService to work with it, as shown below:

public static CrmService CrmWebService
      if (null == m_crmService)
         CrmSdk.CrmAuthenticationToken token = new 
         token.OrganizationName = onfigurationManager.AppSettings[
         token.AuthenticationType = 0;

         m_crmService = new CrmService();
         m_crmService.Url = ConfigurationManager.AppSettings[
         m_crmService.CrmAuthenticationTokenValue = token;
         m_crmService.Credentials = new NetworkCredential(
      return m_crmService;

The preceding code creates a static property called CrmWebService that returns a singleton instance of CrmService. The CrmService is configured for security and accepts the credentials of the logged in user. A real-world application would assign the credentials using System.Net.CredentialCache.DefaultCredentials, but for demonstration purposes the preceding code assumes the user has administrator rights.

Step 3. Create Data-Retrieval Helper Methods

Now you can use the CrmService instance methods to fetch data dynamically in the form of BusinessEntity objects.

BusinessEntity is an abstract class that encompasses all MS CRM entities, which gives you considerable flexibility when fetching MS CRM entity data. Specifically, you can use the class to write generic methods that have no entity type requirements to return records based on dynamic queries.

In addition to methods for basic CRUD operations, the CrmService exposes two other methods:

  1. RetrieveMultiple. Used to retrieve results from a custom query that can contain conditions, filter expressions and link entities.
  2. Fetch. Used to retrieve results from a custom query in XML format.

  3. Author’s Note: The CrmService also has an Execute method that can perform all the above operations, but the individual methods are much faster, so you should use those whenever possible.

The following code illustrates the use of RetrieveMutiple:

public static BusinessEntity[] GetRecordsByEntity(string 
   entityName, string[] columnSet)
   QueryExpression expression = new QueryExpression();
   expression.EntityName = entityName;
   ColumnSet columns = new ColumnSet();
   columns.Attributes = columnSet;
   expression.ColumnSet = columns;

   BusinessEntityCollection entities = 
   if (entities.BusinessEntities.Length > 0)
      return entities.BusinessEntities;
   return null;

The GetRecordsByEntity method returns an array of dynamic entity records by entity name (the contacts in this example). You can choose to return all columns or specify the list of columns to return.

The CRMHelper class needs some additional helper methods to fulfill the design requirements. For this article, the class needs the following additional methods:

  1. GetRecordByEntityId. This is an overloaded method that returns a specific entity record. The contact web part uses this method to populate the contact information. Contact is also a link entity for account. To fetch the account information for the contact and populate the account web part you can create an overloaded form of the method as shown below:
  2. public static BusinessEntity GetRecordByEntityId(
       string entityName, string linkEntityName, 
       string entityAttribute, string linkEntityAttribute, 
       Guid linkEntityId)
       ConditionExpression condition = new ConditionExpression();
       condition.AttributeName = linkEntityAttribute;
       condition.Operator = ConditionOperator.Equal;
       condition.Values = new object[] { linkEntityId.ToString() };
       LinkEntity link = new LinkEntity();
       link.LinkCriteria = new FilterExpression();
       link.LinkCriteria.FilterOperator = LogicalOperator.And;
       link.LinkCriteria.Conditions = new ConditionExpression[] { 
          condition };
       link.LinkFromEntityName = entityName;
       link.LinkFromAttributeName = entityAttribute;
       link.LinkToEntityName = linkEntityName;
       link.LinkToAttributeName = entityAttribute;
       QueryExpression expression = new QueryExpression();
       expression.EntityName = entityName;
       expression.ColumnSet = new AllColumns();
       expression.LinkEntities = new LinkEntity[] {link};
       BusinessEntityCollection entities = 
       if (entities.BusinessEntities.Length > 0)
          return entities.BusinessEntities[0];
       return null;

    As you can see, the QueryExpression for this method accepts a LinkEntity, so the example will specify which contact should be the link entity for account.

  3. GetValueFromProperty. In this example the entity web parts have configurable properties where users can specify the fields that they want to display for the entities. This method takes the property name as a parameter along with the BusinessEntity and returns the value of the property by dynamically looping through the list of properties available for the entity until it finds a match. Listing 1 shows the method implementation.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date