How to debug a sandbox plugin in CRM 2013 online
Preface
Debugging a sandboxed plugin in CRM 2013 online may sound a little obscure at first but don't you deny that it would be really life saver. Maybe you've given up the idea and instead decided to do something that I call poor man's debugging - throwing InvalidPluginExecutionExceptions everywhere and hoping to find a solution? I admit, I used to do that a lot too but this is just wrong. Throwing exceptions is not a solution for debugging, it is solution for handling exceptions and should stay that way.
In this blog post I'm teaching you, step by step, how you can debug a plugin in CRM and save a huge amount of time and effort in the future.
First we go through what you'll need. You probably own all necessary components already since you are a CRM programmer.
Then I explain an example scenario that I'm using while going through this debuggin process.
You may now close all other tabs in your browser. This blog post contains all you need in order to debug a plugin in CRM Online.
What you need
- Plugin to debug
- CRM 2013 online (although this might work for on-premises as well)
- CRM 2013 SDK
- Visual Studio (I'm using Visual Studio 2012)
Scenario
Let's pretend we are developing a plugin that has to generate an email address for a contact. Email address is generated by using contact's full name and by adding a fixed "@exampledomain.com" to the end.
We've created a plugin that'll be fired always when a contact is created or updated (running a plugin on every update is not preferred in this case but just play along).
Both steps are registered on pre-operation stage of execution.
Below is my plugin code. Note that I've divided some of the common plugin logic to its own class (Plugin.cs) which we are inheriting in our actual plugin.
So what happens there is we take target entity's full name property and add "@exampledomain.com" to it. Then we assign that new email string to entity's emailaddress1 property. Since this is a pre-operation plugin we don't have to call update.
This plugin works well on create because full name is business required field. But for some reason on update this is not working... Well we all know the reason already, but let's pretend we don't.
It's debugging time!
Debug a plugin
Step 1
Open your plugin registration tool. It is located in SDK\Tools\PluginRegistration folder.
Step 2
While plugin registration opened you want to install something called a profiler. All you need to do is to press "Install Profiler" and wait for a moment.
When the profiler is installed (when you see "Profiler Installed Succesfully") press Ok.
Note: This (profiler) creates a new managed solution to CRM. As I was writing this tutorial something weird happened (CRM happened) and my plugin registration tool lost a connection to CRM. I wasn't able to connect CRM for a little moment so I went and deleted that managed solution from CRM manually. I don't know if it was only coincidence or if it actually helped but everything worked normally after that. So that's something to keep in mind for sure!
Step 3
Choose a plugin and a plugin step you want to debug.
As described in the scenario our plugin is crashing on update event. So we want to debug our plugin on update step / event.
Highlight a correct step and press "Start Profiling"
Step 4
Set up profiler settings. You may want to leave them as they are. Press OK when ready.
Step 5
Once you've started the profiler you have to raise an event that will fire the plugin. In our case that means we have to update an existing contact.
I'm updating Fernando Alonso's job title to 1st driver which will raise an update event but will also crash our plugin (well, you know why don't you). When that happens a profiler gives us a log file that we have to download and store locally for further use.
Download the log file and then close business process error popup by pressing ok.
Step 6
Go back to your plugin registration tool and press debug.
Step 7
Replay Plug-in Execution popup opens on the screen. There are three fields that has to be filled.
1. To "Profile" field search the log file you just downloaded in step 5.
2. Assembly Location: To this field you need to search for your plugin's DLL file. When you are building your projects in Visual Studio it creates a debug folder under a bin directory. Use the DLL file from the debug folder.
3. Select the right plugin
Do not press Start Execution yet
Step 8
Open Visual Studio and your plugin solution / project if not already opened.
Add break points to your code.
So from now on it works as normal debugging. I add one break point to beginning of the plugin.
Step 9
Attach Visual Studio to PluginRegistration process.
When Visual Studio and your plugin solution / project is opened go to Debug -> Attach to process.
Step 10
Select PlugingRegistration.exe from the list. If you have more than one opened at the same time make sure to select a right one. Then press attach. Your Visual Studio will go to a debug mode.
Step 11
This is the moment we've been waiting for. Go back to plugin registration tool and press "Start Execution".
Step 12
Now you can actually debug your code, step by step!
Step 13
Enjoy! When you're done stop profiling and remove profiler. I'm sure you'll find out how, just do the steps in reverse order :)
Final step
Find the problem and fix it.
For those who wonder the problem was that target entity didn't carry full name property because it was not changed and thus not passed for the execution context. That's why we got "The given key was not present in the dictionary"
Final words
Thank you for reading. Hopefully I was able to clear things out. This may seem little overkill but trust me on this, it'll save you a lot of time on a long run.
Please do not pay too much attention to code in this post. I wanted to keep it simple as possible. Don't do what I do, do what I say :)