I’ve become a big fan of extension methods over the years to make my development work in Dynamics CRM, easier to implement and easier to read (i.e., easier to support.) Extension methods allow us to, exactly as the name implies, extended an existing class or interface with new methods – its a simpler version of implementing a derived class.

Anyone familiar with developing .NET code for Dynamics CRM will be familiar with the `IOrganizationService` interface and its varied implementations. While in most cases I’m a fan of implementing my own version of IOrganizationService, often for code-base simplicity I’ll rely on some extension methods for my code.

Two of my favorites are to simplify the calling of the IOrganizationService.Retrieve() method. As you are aware the IOrganizationService.Retrieve() method requires an instance of the ColumnSet() class. It also has the limitation that will only return an Entity() object, not an early-typed object such as Account(). Now, both of these are minor but I find the resulting code to be less than elegant, and when code is less than elegant it will be more difficult for someone to read in the future.

Hence if I have to write this:

_service.Retrieve(Account.EntityLogicalName, recordId, new ColumnSet(true)).ToEntity<Account>();

I would prefer to write this:

_service.Retrieve<Account>(Account.EntityLogicalName, recordId);

Or if I have to write this:

_service.Retrieve("account", recordId, new ColumnSet("accountid", "name", "primarycontactid"));

I would prefer to write this:

_service.Retrieve("account", recordId, "accountid", "name", "primarycontactid");

//If you define your field name strings (which you should!)
_service.Retrieve(Account.EntityLogicalName, recordId, Account.Fields.AccountId, Account.Fields.Name, Account.Fields.PrimaryContactId);


I find, in my opinion at least, the second options to be less to type and generally quicker to understand (although, admittedly, you have to get used to not seeing new ColumnSet() everywhere.

Btw, the use of generated field names (in the second line of the second preferred example) are a result of the XRM Early Bound Generator, which I highly recommend using, by Daryl LaBar. While this may appear to be more to type with the benefit of Intellisense it will be less typing and fewer errors.

You can implement these two extension methods by adding a public static class to your CRM projects. Once you have this class it can become a valuable utility class to add additional extension methods.