Monday, June 29, 2009

Adding a Custom Button to Advanced Find in CRM 4.0

Have you ever observed how powerful Advanced Find dialogue can be in the right hands? Well apart from looking at the customized result set and providing total control to filter the data Advanced Find can be used for some really cool tricks. Recently, I had to face a situation where I needed to turn Advanced Find into some bit simpler find by removing some functionalist and by adding a simple button on the Grid Toolbar.

I managed it somehow in few hours and thought to share it with you, just in case you also fall in the same trap some day. So here it goes, just note that, modifying Advanced Find is NOT at all supported and I DO NOT take any responsibility what so ever if you follow the steps I've mentioned below and end up with any kind of damage[mental\physical\financial\social\...].

Here are steps to add a button to advanced find window
1. Go to CRM Installation directory and navigate to \CRMWeb\AdvancedFind\. Mostly you will have to go to C:\Program Files\Microsoft Dynamics CRM\CRMWeb\AdvancedFind.
2. Take a back up of AdvFind.aspx.
3. Open AdvFind.aspx in any text editor. [Visual Studio is recommended though].
4. locate the first </form> tag.
5. Copy-paste the following line : <input type="button" onclick="Alert('Hey!!!... I am in Advanced Find Window...')" value="Click Me!!!" />
6. Save and Close the file.
7. Go to CRM and click on Advanced Find... Dialogue. You will see a long ugly button on top. Click on it to see the message.
This is a simple approach I've followed. There can be tons of things you can do. You can skin it to look like the real CRM buttons and then replace the alert message with some really useful and complex logic.

Anyways, I took it little further and added the following script and removed the button from the previous step.

<script language="javascript" type="text/javascript">
function DoIt() {
var template = document.getElementById("btnEditProperties");
var newNode = document.createElement(template.tagName);
newNode.setAttribute("class", "ms-crm-Menu");
newNode.setAttribute("tabIndex", "-1");
newNode.setAttribute("title", "Visit Metrix...");
var magic = "<SPAN class=ms-crm-Menu-Label><A class=ms-crm-Menu-Label tabIndex=-1 onclick='return false;' href='javascript:onclick();' target=_self><IMG class=ms-crm-Menu-ButtonFirst tabIndex=-1 alt='Visit Metrix....' src='/_imgs/vieweditor/16_viewProps.gif'><SPAN class=ms-crm-MenuItem-TextRTL tabIndex=0>http:\\\\</SPAN></A></SPAN>";
newNode.insertAdjacentHTML("afterBegin", magic);

and I got this :

Also you might want to add a little delay in the execution of the script by using window.setTimeout method otherwise it will give error in case the grid is not loaded by the time script is called.


Ahmad Pirani said...


Is it possible to populate an attribute's value with dynamic value on run time?
Example, if I have a string field where I store GUID. Now in advanced fine, I would like to select this attribute and for enter value, I would like to get values dynamically from runtime. Say GUID or current user or something like that.

Thanks in advance.

Chinmay said...

Hi Ahmad,

You mean to get a value[filter maybe] from CRM form which calls this dialog right? I am not sure, but I think it can be done.


Ahmad Pirani said...

Thanks for your response, Chinmay.
I had an string attribute that stores Guid of users (this attribute is not a lookup). Hence, in advanced find when you select this column, you will be asked to specify a string value. You wont be able to select "Current User" as you would be able to do for a Owner or Created by lookup column.
So, I created a normal fetchxml using advanced find, and modified the fetchxml directly in the database. I changed <attribute name="myColumn" operator="eq-userid" />. This solved my problem.