Useful Tool for Writing Plugin in Dynamics CRM 2011 – Part 1

Vincent Zhong, 25 July 2012

Since I started my journey with Microsoft Dynamics CRM 2011 last year, I have done a few plugins. I found that one of the common problems for a Dynamics CRM newbie to write a plugin is where to start, and what can I use in my C# code. In this blog post I will show a diagram and use an example to demonstrate how to use it to speed up your plugin development.

Useful Tool for Writing Plugin in Dynamics CRM 2011 Part 1

In my previous blog posts, I have shared with you the diagrams I done to summary the functions you can use to work with Xrm.Page Object Model (mainly for my personal use first, then I think I should share it), I think is a good idea to do one for Plugin Development, so that I can reference to certain points when I am doing a plugin myself.

Since I have done two blog posts on how to write a naming plugin from scratch, which are here: Part1, Part2, I won’t go into too much detail on how to set up the project, and how to deploy your plugin. This blog post will focus on the diagram and how to use it.

Here is the diagram I made.

 Useful Tool for Writing Plugin in Dynamics CRM 2011 Part 1 

You can find the full size version here.

The rest of the blog post will be a simple example on how to use it, and I will show you some more ways to use the diagram on my next blog post.

After you set up your plugin project, you plugin class will look something like this:

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Microsoft.Xrm.Sdk;

namespace
MyPlugin
{
    public class Class1 : IPlugin
    {
        private IPluginExecutionContext context = null; 

        public void Execute(IServiceProvider serviceProvider)
        { 

        }
    }
}

When I first start writing plugin, I always a bit confused when I come to here, what entity I need to use here, and their names seem so long and hard to remember. And I don’t want to keep opening another project to copy and paste from there. With the diagram I can just open this image and I pretty much know what entities/functions I can use here. I have highlighted the 3 parts that you need to start with.

 Useful Tool for Writing Plugin in Dynamics CRM 2011 Part 1

From the diagram we can see, we need to instantiate 3 Interfaces.

IPluginExecutionContext let us get things we need for the Plugin execution environment itself, like the InputParameters of the Plugin, or the PreEntityImage, which is a copy of the entity before the execution of the Plugin.

IOrganizationServiceFactory let us execute CRM actions, like Update or Delete. It is very useful because we don’t just want to get data from CRM, but also want to action on CRM.

ITracingService is a must have tool for debugging. From the log file we can trace where the plugin failed.

That’s for this blog, hope you found it useful and you can use it in your own project. 

* Image from: http://www.clker.com/clipart-43053.html