Microsoft Dynamics CRM Audit Plugin Tool
Database auditing tools are extremely valuable when you need to be able to determine who made a certain change or when trying to figure out how a particular value was set. Microsoft Dynamics CRM does not have any native auditing beyond storing who created a record and who modified it. There is no way to see which fields have changed and what their previous values were.
However, we can easily create our own auditing plugin tool and easily audit all fields or just specific fields for specific entities. Note: All code samples and documentation contained in this post assume that you have access to the CRM server directly, that the user account running the CRM Async Service has permissions to read the registry on the CRM server, and that the Prefix in the Customization tab in System Settings is the default “new.”
First of all, we need an entity to store the audit records. We could store the audting records in their own SQL database; however, we have chosen to store the records in CRM so that we can use native features of CRM to find data, such as Advanced Find and CRM Reports. Download the custom audit entity here. Refer to CRM documentation on how to import and publish the custom entity.
Secondly, we will store a few configuration settings in the registry on the CRM server. Download the .reg file here and make the necessary changes to it before you import it into your registry.
Lastly, we need to register the plugin. We have used the free plugin developer tool that comes with the CRM SDK and it can also be downloaded in its compiled version at the end of this post. You can download the source code for the plug-in here and screen shots are below.
Steps to register the plugin
Step 1: Register the .dll file.
Step 2: Browse to the .dll file and then register the AuditPlugin class into the database. Note, it is best to do this directly on the CRM 4.0 Application server.![]()
Step 3: Plugin should register successfully. If you get any errors, refer to CRM support forums for troubleshooting suggestions.
![]()
Step 4: Register a Create step. Fill in the form as shown in the screen shot below. As you are typing you will notice that there is some auto-suggest or intellisense built-in. As shown in this example, we are registering the Audit Plugin to run anytime an account is created in CRM and the audit plugin will have access to all attributes on the account record that is created.
![]()
Step 5: After creating the Create step your screen should look like the following.
![]()
Step 6: Register the Update event. As shown in the screen shot below we are telling CRM we want our plugin to run any time any attribute on an account record changes. What will happen is each time a user clicks Save or Save and Close on an account in CRM, if any of the attributes have changed they will be passed to our audit plugin which will create audit records in CRM.
![]()
Step 7: This steps shows how you need to register Post and Pre Images for each Step as well. This allows the audit plugin to know what the value was before it changed and what it is after the change.
![]()
Step 7b: A sample of creating a Pre image. Note, you will need a Pre and Post image for all Update steps and only a Post image for any Create steps.
![]()
You can see that it is pretty simple to register the plugin to be able to audit any entity or even any attribute that you want.
Resources:
CRM Audit Plugin Package (includes everything mentioned)
posted in Microsoft .Net, Microsoft Dynamics CRM | 13 Comments