Set custom Full Name format in CRM 2011 with Plugin

Paul Nieuwelaar, 25 August 2013

In Dynamics CRM 2011, by default on Contacts, Leads, and Users, the format for ‘Full Name’ is defined as ‘First Name Last Name’. We have the ability to change this format in the Administration -> System Settings, however the options are rather limited, and do not allow for custom formats outside of the provided few.

 Set custom Full Name format in CRM 2011 with Plugin

If the format you’re after is not in this list, you can write a plugin to set the Full Name, which will override CRM’s formatting.

To do this, simply create a plugin to run on create and update of contact, lead, and/or user, and set the update step to filter on the attributes you want to make up the full name. For example, in my case I want the Full Name to be “Salutation First Name Last Name” so the plugin will need to filter on all 3 attributes.

The plugin will need to run synchronously, and in the pre-operation stage, otherwise CRM will override the full name after we’ve set it.

An example of the plugin code can be seen as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tester.Data;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace Tester.Plugins
{
    /// <summary> 
    /// Override the CRM Fullname format on a contact to be "Salutation First Name Last Name". 
    /// 
    /// Plugin steps: 
    /// create - contact - pre-operation - synchronous 
    /// update - contact - pre-operation - synchronous - filtering attributes: firstname, lastname, salutation 
    /// </summary> 
    public class Tester : IPlugin 
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            if (!context.InputParameters.Contains("Target")) { return; }
            Entity target = context.InputParameters["Target"] as Entity;
            if (target == null) { return; }

            SetFullName(target, context, service);
        }
        private void SetFullName(Entity target, IPluginExecutionContext context, IOrganizationService service)
        {
            Entity contact = target;

            //if it's update, and 1 or more values arent in target- get the latest data 
            if (context.MessageName.Equals("update", StringComparison.InvariantCultureIgnoreCase) &&
                (!target.Contains("salutation") || !target.Contains("firstname") || !target.Contains("lastname")))
            {
                Entity current = service.Retrieve(target.LogicalName, target.Id, new ColumnSet("salutation", "firstname", "lastname"));
                contact = MergeEntities(target, current);
            }

            //get the values from target or current depending on whether it was in target 
            string salutation = contact.GetAttributeValue<string>("salutation");
            string firstName = contact.GetAttributeValue<string>("firstname");
            string lastName = contact.GetAttributeValue<string>("lastname");

            //join the salutation, first name, and last name (ignoring nulls) 
            string fullName = string.Join(" ", new string[] { salutation, firstName, lastName }
                .Where(a => !string.IsNullOrWhiteSpace(a)));

            //update target, overriding whatever CRM sets 
            target["fullname"] = fullName;
        }

        private Entity MergeEntities(Entity primary, Entity secondary)
        {
            Entity merged = new Entity();
            merged.Attributes.AddRange(primary.Attributes);

            secondary.Attributes.ToList().ForEach(a =>
            {
                if (!merged.Contains(a.Key))
                {
                    merged[a.Key] = a.Value;
                }
            });

            return merged;
        }
    }
}
 

Once the plugin is registered, you can go in and update some names, and watch the Full Name get set to our new format.

Set custom Full Name format in CRM 2011 with Plugin