Writing a Naming Plugin for Dynamics CRM 2011 - Part 2

Vincent Zhong, 18 May 2012

In my last blog post, I have talked about why we need a Naming Plugin and we have done some customization to our Meeting entity for, to remove the Name field from the form. In this blog post I will show you how to write the Naming Plugin.

As stated before, this blog is more showing you the logic and code for generating the entity Name using a Plugin, for general Plugin development or how to create a Plugin from scratch, you can checkout this tutorial provided by Microsoft.

Writing a Naming Plugin for Dynamics CRM 2011 Part 2*

So I have created a new Plugin class called “TestNamingPlugin.cs”.  The main purpose of this Plugin will be set the Name of Meeting to “With - Time - Place” format.

At the beginning of the class, just some standard checking:
 Writing a Naming Plugin for Dynamics CRM 2011 Part 2

Then we can check it is a Create or Update form, as the behaviour will be slightly different between them. Then call a different function.
 Writing a Naming Plugin for Dynamics CRM 2011 Part 2

In our HandleUpdate function, we just retrieve the entity record, then call the HandleGeneric function.
 Writing a Naming Plugin for Dynamics CRM 2011 Part 2

Here we have done a little trick, as the step we registered is “Pre-operation”, so only the changed attributes will be in our entity, so we need to create another entity with the same type, then retrieve any other information we need from database, then merge those two entities. The merge function is below:

 Writing a Naming Plugin for Dynamics CRM 2011 Part 2

In our Handle Generic function, we will get the details of the fields we will use to make up our Name.
 Writing a Naming Plugin for Dynamics CRM 2011 Part 2

Here are two helper functions we used in our code above:
 Writing a Naming Plugin for Dynamics CRM 2011 Part 2

Now if you can build this Plugin, and register it using Plugin Registration Tool, check out this link if  you are not quite sure. 
 Writing a Naming Plugin for Dynamics CRM 2011 Part 2

Now after we registered this plugin, and save the record, the name of the record should looks like this: 

Writing a Naming Plugin for Dynamics CRM 2011 Part 2

That’s all for Naming Plugin, hope you find it useful in your next project.  

*Image from http://www.dixel.co.il/logo_plugin.jpg