Open CRM 2011 Lookup window in a custom html page

Posted: October 2, 2012 in CRM 2011, Jscripts

Open CRM Lookup window in modal dialog

In my example i have added a html webresource which contains a textbox, lookup image and hidden field to store the GUID of the lookup record. On click of the image button i need to open a CRM looup window in a modal dialog.

Here is the onclick jscript function code

 function OpenLookup() {
            // prepare the crm url
            var customServerURL = document.location.protocol + "//" +;
            // In my example im using contact lookup. so its objectcode is 2
            var objectCode = "2";
            var url = customServerURL + "/_controls/lookup/lookupsingle.aspx?class=null&objecttypes=" + objectCode + "&browse=0&ShowNewButton=0&ShowPropButton=1&DefaultType=0";
            var lookUp = window.showModalDialog(url, "entity", "width=500,resizable=1,height=400,menubar=0,status=1,location=0,toolbar=0,scrollbars=1");
            if (lookUp != null) {
                var txtLookup = document.getElementById("txt_Lookup");
                // Add the lookup name in input text
                txtLookup.value = lookUp.items[0].name;
                // Add Guid of the record in a hidden field
                document.getElementById("hdn_Guid").value = lookUp.items[0].id;

Note : I have used CRM 2011 online version for the above example. You may have to add the organization name in preparing lookup url for CRM on premise version.
Here is the sample for on premise version

 var orgName = window.parent.Xrm.Page.context.getOrgUniqueName();
var serverUrl = window.parent.Xrm.Page.context.getServerUrl() + "/main.aspx";
var objectCode = "2";
var url = "http://" + + "/" + orgName + "/_controls/lookup/lookupsingle.aspx?class=null&objecttypes=" + objectCode + "&browse=0&ShowNewButton=0&ShowPropButton=1&DefaultType=0";
  1. Vaibhav says:

    Hi Lakshman,

    The code doesn’t work with either Rollup 12 or 13, can you pls check?



  2. Phil says:

    Hey your post it’s very helpful but I have a problem when I try to get the value in this line:

    var lookUp = window.showModalDialog(url, “entity”, “width=500,resizable=1,height=400,menubar=0,status=1,location=0,toolbar=0,scrollbars=1”);

    The value I get is undefined, can you please tell me what am I missing?

  3. sarah says:


    How do i get lookup value if I am poping up the lookup from javascript. Any thoughts

  4. Aaron Carpenter says:

    Thanks for your post on this. I am trying to implement this solution on an On-Premise installation. When the dialog pops up, all I see is the header of the dialog (“Look Up Record”) with the text description and the Ok, Cancel & Remove Value buttons displayed right below that. The section of the dialog where you would search for the competitor and select the desired one is completely missing. If I update your script to use…) instead of window.showModalDialog(…), then the dialog displays correctly in a new window, but doesn’t pause the script execution until a lookup value is selected so this wont work. Do you have any suggestions or things that I can try. I can send over a screen shot if that would be helpful.

  5. Pooja Bhagwat says:

    I have aspx application . on which i have added one text field and Image button (like lookup control in crm ) . i want to open lookup window on click of taht button . i tried your code . sets onClientClick event to your funtion. but control is not coming back to var lookup. page is getting postbacked.
    Please help me in this issue. below is my code
    function OpenUserLookup()
    var url = newURl + “/_controls/lookup/lookupsingle.aspx?class=null&objecttypes=” + 8 + “&browse=0&ShowNewButton=0&ShowPropButton=1&DefaultType=0”; var lookUp = window.showModalDialog(url, “entity” “dialogWidth:500px;resizable:1;dialogHeight:650px;scroll:1;resizable:1”);

    return false;

    Button –


    • Felipe says:

      That might be because you’re running that aspx outside of the context of CRM. Try to put your website or page into an CRM IFrame.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s