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:

<Entity>
<MenuBar>
<Additional Tags Here></Additional Tags Here>
</MenuBar>
<ToolBar>
<Additional Tags Here></Additional Tags Here>
</ToolBar>
<NavBar>
<Additional Tags Here></Additional Tags Here>
</NavBar>
<Grid>
<MenuBar>
</MenuBar>
<Buttons>
<Button Title=""...etc/>
</Buttons>
</Grid>
</Entity>

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="window.open('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.

2 comments:

Anonymous said...

Calling an undocumented crm page function is risky because it can change in the next version.

The client-side SDK includes the following instructions to get the GUID values for selected items in the grid.

You need to query the window.dialogarguments



See http://msdn2.microsoft.com/en-us/library/aa682400.aspx

Remarks

In the example below, the IDs of selected items are passed into the opened dialog in forms of an array. Access to the array is through window dialog arguments.

Example

The following code sample shows the use of the Grid element.


<Grid>
<MenuBar>
<Buttons>
<Button Title="Grid" ToolTip="Info on Test"
Icon="/_imgs/ico_18_debug.gif"
Url="/_resources/mydialog.aspx"
WinParams=""
WinMode="2" />
</Buttons>
</MenuBar>
</Grid>

Anonymous said...

How about figuring out the entity type for those selected rows?

Post a Comment