Friday, 3 April 2015

Alter Lookup Field/Column Displayed Value In the CRM View Using C#

When the users go to CRM and access the Entity View, the objective is they want to see the list of complete data, that they don’t want to click and enter each record, one by one.


Often we can see a field showing same value, but in fact it should refer to different record.
Here is for example:


As we can see here, there are many Cases referring to same Customer Name (for example: Adventure Works), but actually, are they? Are they the same ‘Adventure Works’ or not?

Well, if we click one by one, we will know that they are different, because in fact we have  a lot of Adventure Works around the world (can be in Jakarta, Sydney, Singapore, Canada, etc.) or we can just add new columns to display, but it means consume another column space, and imagine every time you need to add the columns to the view, including your personal view.

Now, let’s tweak it little bit, I want to see the Case from which Customer, really, I want to know which the customer, exactly? Is that from Jakarta, Sydney, or any other branch.

Expected Result

I want to get like this:

Case 1        Adventure Works [Jakarta, Indonesia]
Case 2        Adventure Works [Sydney, Australia]

Not only showing ‘Adventure Works’

I want to concatenate the multiple fields into single lookup field column.

The Code

Here is the Sample C# Code to manipulate the lookup displayed value.

public void Execute(IServiceProvider serviceProvider)
     #region must to have

     IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

     IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

     // Create service with context of current user
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

     //create tracing service
     ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));


     if (context.OutputParameters.Contains("BusinessEntityCollection"))
          var retrievedResult = (EntityCollection)context.OutputParameters["BusinessEntityCollection"];
          foreach (Entity entity in retrievedResult.Entities)
              //retrieve the CustomerId Entity Reference
              if (entity.Contains("customerid"))
                  EntityReference erCustomerId = (entity.Attributes["customerid"] as EntityReference);

                  if (erCustomerId != null)
                      //retrieve the customerid detail
                      Entity enCustomer = new Entity();
                      enCustomer = service.Retrieve(erCustomerId.LogicalName, erCustomerId.Id, new ColumnSet("address1_city", "address1_country"));

                      //retrieve the City and Country detail
                      string strCity = enCustomer.Contains("address1_city") ? enCustomer["address1_city"].ToString() : string.Empty;
                      string strCountry = enCustomer.Contains("address1_country") ? enCustomer["address1_country"].ToString() : string.Empty;

                      //alter the displayed value for column name
                      erCustomerId.Name = string.Format("{0} [{1}, {2}]", erCustomerId.Name, strCity, strCountry);

How to Register Your Plugin

Please register your Plugin with the following config:


Message: RetrieveMultiple

Primary Entity: incident

Event of Execution: Post-Operation

And it is Synchronous Plugin.


Now, see the result here:


*As we can see, you can see the additional information that is in separated fields to be displayed in single column.

This is also can be workaround for the CRM Limitation to only get the column from up to one level related entity.

So, let’s say you have Customer as the Lookup field, then you can only get the Columns from the Account/Contact, you cannot get the Column from the Lookup detail of the Account, for example: Originating Lead's Columns, Account Owner’s columns, etc.

It also does not consume to much space and you can put another detail, concatenate multiple columns into single column display.

Hope this helps!



  1. Hi,
    I followed your code and i am getting the below error in opportunity entities.
    Unexpected exception from plug-in (Execute): Plugins_Util1.ActiveViewModify: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.


    1. I think your error is in this String.Format related
      erCustomerId.Name = string.Format("{0} [{1}, {2}]", erCustomerId.Name, strCity, strCountry);

  2. What does "BusinessEntityCollection" refer to?


My Name is..