CRM 2011 Plugin Template

Here is the template to get you started on plugin development in CRM 2011:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using System.ServiceModel;
using Org.Entities.Crm; //this is the generated entities.cs for early bound
 
namespace My.Plugins
{
    //insert plugin comment
    public class AccountPlugin: IPlugin
    {
        ITracingService tracingService = null;
 
        public void Execute(IServiceProvider serviceProvider)
        {
            // Extract the tracing service.
            tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            if (tracingService == null)
                throw new InvalidPluginExecutionException("Failed to retrieve the tracing service.");
 
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
 
            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.
                Entity entity = (Entity)context.InputParameters["Target"];
 
                // Get a reference to the Organization service.
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
 
                try
                {
                    // Plug-in business logic goes below this line.
                    // Invoke organization service methods..
                    // do whatever you have to do..  
                }
                catch (InvalidPluginExecutionException iex)
                {
                    tracingService.Trace(iex.Message);
                    tracingService.Trace(iex.StackTrace);
 
                    throw iex;
                }
                catch (FaultException<organizationservicefault> ex)
                {
                    tracingService.Trace(ex.Message);
                    tracingService.Trace(ex.StackTrace);
 
                    throw new InvalidPluginExecutionException(string.Format("An error occurred in the {0} plug-in.", this.GetType().Name), ex);
                }
                catch (Exception e)
                {
                    tracingService.Trace(e.Message);
                    tracingService.Trace(e.StackTrace);
 
                    throw new InvalidPluginExecutionException(string.Format("An error occurred in the {0} plug-in.", this.GetType().Name), e);
                }
            }
        }
    }
}


To get the service context of your organisation :
1
OrgServiceContext serviceContext = new OrgServiceContext(service);


Important Notes:

PRE PLUGINS
  • You don't need to call SaveChanges because the entity can be updated before it is saved into database.
  • For pre-update, entity will only have attributes that are changed. To get the other attributes that don't change, you can query those attributes using the service context.

POST PLUGINS
  • You need to call SaveChanges because the entity has been saved into database.
  • Entity will have all updated attributes when retrieved using service context.
  • When doing the post update of an entity that update the same entity attributes, loop will occur. To avoid this you can use context.Depth to check the loop count.

OTHERS
  • For delete message, the context.InputParameters["Target"] is an EntityReference instead of Entity.
  • You can check the context message using context.MessageName

Hope this helps,
Andreas

Comments

Popular posts from this blog

SharePoint 2013 anonymous access add attachments to list item

Sitecore custom publish agent from specific node and at a specific time