Filtering Lookup Data in Dynamics CRM 4

March 13, 2012 12:00 am
Andri Kasta Marengga

Filtering Lookup Data in Dynamics CRM 4

First, we have to make sure filtering lookup is supported by your Dynamics CRM. Open lookupsingle.aspx inside <CRM site folder>\_controls\lookup\ folder. If filtering lookup has not been supported, add these code snippet to the top of the page. As usual, never change a file without a backup.

<script runat="server">

protected override void OnLoad( EventArgs e )
crmGrid.PreRender += new EventHandler( crmgrid_PreRender );

void crmgrid_PreRender( object sender , EventArgs e )
// As we don't want to break any other lookups, ensure that we use workaround only if
// search parameter set to fetch xml.
if (crmGrid.Parameters["search"] != null && crmGrid.Parameters["search"].StartsWith("<fetch"))
crmGrid.Parameters.Add("fetchxml", crmGrid.Parameters["search"]);

// searchvalue needs to be removed as it's typically set to a wildcard '*'

// Icing on a cake - ensure that user cannot create new contact outside of the account
// and then select it.
this._showNewButton = false;


After that, let’s play with Dynamics CRM Forms. To filter lookup data, you have to know at least these criteria:

  • What field you’ll filter
  • Lookup destination entity
  • Search criteria

Here I give you an example to filter Primary Contact lookup field on Account.

// Which field we will filter
var field = crmForm.all.primarycontactid;

// Ensure that search box is not visible in a lookup dialog
field.lookupbrowse = 1;

// Prepare fetch xml to filter lookup data
var filterStr="<fetch mapping='logical'> "+
"<entity name='contact'> "+
"<filter> "+
"<condition attribute='parentcustomerid' operator='eq' value='" + crmForm.ObjectId + "' /> "+
"</filter> "+
"</entity> "+

// Pass fetch xml through search value parameter

Since this method isn’t supported by Microsoft, I can’t guarantee this is 100% safe.

, , , ,

No comments yet.

Leave a Reply