Friday, December 08, 2006

Toolbar Buttons on the Grid/List View

This customization has been one I've been trying to figure out for some time. After checking some different posts on different MSCRM groups I finally pieced together everything I needed to be able to grab the GUID of the selected record(s) from the list.

The best thing about this customization is that it's totally supported and you will only have to change your code if Microsoft changes their code in one of their .js files.

So, let's get into actually applying this customization. First things first, let's get into the isv.config file. Within the Entity tag you want to get into the Grid tag. The isv.config file I was working with did not have this tag and I came across how to add a button in the SDK. So, the XML structure should look like this:

<Additional Tags Here></Additional Tags Here>
<Additional Tags Here></Additional Tags Here>
<Additional Tags Here></Additional Tags Here>
<Button Title=""...etc/>

Add the button just like you would anywhere else in CRM. Please refer to the SDK for additional switches that can be added to the Button tag.

So, that's adding the button. One other note about adding a button here to the Grid is that is shows up in all Grid views for the entity. This includes, the main CRM page, Advance Find, and Associated Views. So, your button shows up everywhere.

Now that we've got a button, we can make it go to a custom ASPX page and basically do whatever we want. The hardest part of this passing the GUID of the item(s) you have selected in order to take action against them. Now, to figure this out I took numerous suggestions off of different posts but the one that really "sealed the deal" for me was from Gunady. I'm not sure if he got his information from someone else or not but I have to give credit where credit is due and he gave me the bit I needed to get my customization to work.

The key is this little bit of JavaScript: getSelected('crmGrid'). This gives you the GUID of whatever items you have selected in the CRM Grid. So, if you have one selected this code will pass the GUID with the curly braces "{}" around it. If multiple items are selected then each GUID will be separated by a comma ",".

So, my button code looks like this:

<Button Title="ButtonTest" PassParams="1" ToolTip="Test" WinMode="0" JavaScript="'http://crm/GridButton/GridButton.aspx?oid='+ getSelected('crmGrid'), 'test','top=100,left=175,width=500,height=300,menubar=no,
resizable=yes');" />

Once I had that all I had to do was pull the parameter in my custom ASPX page code and then do whatever I wanted with it. To pull the parameter I used this line of code in the Page_Load method of my Visual Studio project:

objoid = Request.Params["oid"];

Next thing I had to do was separate the GUIDs from each other in the case of a multiselection. I used the Split method from the String class to do that using the comma "," and through my values into an array. Once I had the array built I then just cycle through a while loop (checking against the length of the array), strip the curly braces "{}" from my GUID (because the GUID method just wants the numbers) and then use each GUID in the array to do whatever customization I want.

I know I spent quite a bit of time researching this so I hope my findings are able to help someone else out in their CRM customizations.

Happy Coding!

David Fronk
Dynamic Methods Inc.