Home > Dynamics CRM > CRM 4.0 – Obtain Plugin Event Information When The Event Is Fired (Code)

CRM 4.0 – Obtain Plugin Event Information When The Event Is Fired (Code)

I spotted this in the CRM 4.0 SDK and thought I would post it up as a reference. As the code states, this sample code "outputs a report on the context on which this plug-in is attached.  You can attach this plug-in to any entity or event and it will output a report on the event." This could be quite a handy reference if you wish to obtain this type of information on your custom Plugin for logging or custom auditing purposes.

 

   1: using System;
   2: using System.IO;
   3: using Microsoft.Crm.Sdk;
   4: using Microsoft.Crm.SdkTypeProxy;
   5:  
   6: namespace Microsoft.Crm.Sdk.Reference.Plugins
   7: {
   8:     /// <summary>
   9:     /// This sample outputs a report on the context on which this plug-in is attached.  You can attach this
  10:     /// plug-in to any entity or event and it will output a report on the event.
  11:     /// </summary>
  12:     public class PluginRef: IPlugin
  13:     {
  14:         public void Execute(IPluginExecutionContext context)
  15:         {
  16:              using (TextWriter log = TextWriter.Synchronized(File.AppendText(@"C:\Crm_Sdk_Samples\PluginReport.txt")))
  17:              {
  18:                 log.WriteLine("Plugin Fired: {0}", System.DateTime.Now.ToString("MM/dd/yyyy hh:mm tt"));
  19:                 log.WriteLine("=====================================================================");
  20:                 
  21:                 // Entity Context Related
  22:                 log.WriteLine("Primary Entity Name:\t\t\t{0}", context.PrimaryEntityName);
  23:                 log.WriteLine("Secondary Entity Name:\t\t\t{0}", context.SecondaryEntityName);
  24:                 log.WriteLine("Message Name:\t\t\t{0}", context.MessageName);
  25:                 log.WriteLine("Caller Origin:\t\t\t{0}", context.CallerOrigin.ToString());
  26:                 log.WriteLine("Is Executing In Offline Mode:\t{0}", context.IsExecutingInOfflineMode.ToString());
  27:                 
  28:                 // Security Related            
  29:                 log.WriteLine("Business Unit Id:\t\t{0}", context.BusinessUnitId.ToString("b"));
  30:                 log.WriteLine("Organization Id:\t\t{0}", context.OrganizationId.ToString("b"));
  31:                 log.WriteLine("User Id:\t\t\t{0}", context.UserId.ToString("b"));
  32:                 
  33:                 // Space this entry and close the log
  34:                 log.WriteLine();
  35:                 log.WriteLine();
  36:             }
  37:         }
  38:     }
  39: }

Greg Olsen
Yellow Duck Guy

Advertisements
Categories: Dynamics CRM
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: