Wednesday, January 07, 2015

HowTo: Open standard lookup window and get selected record(s)

If you are apologist of 100% supported solutions for Dynamics CRM this is a good place to close this article because it describes approach that will allow to open standard lookup dialog window of CRM and get selected values from it. Of course that customization is unsupported.
To use standard lookup control you can use following code:

function OpenLookup() {
    var lookupurl = "/_controls/lookup/lookupinfo.aspx?" + 
        "AllowFilterOff=0&DefaultType=2&DisableQuickFind=0&DisableViewPicker=0&IsInlineMultiLookup=0" +
        "&LookupStyle=single&ShowNewButton=1&ShowPropButton=1&browse=false&objecttypes=2";
    var dialogwindow = new Mscrm.CrmDialog(Mscrm.CrmUri.create(lookupurl), window, 500, 500);
    dialogwindow.setCallbackReference(function (result) {
        alert(result.items.length + " records were selected");

        for (var i = 0; i < result.items.length; i++) {
            alert(result.items[i].typename + " with name " + result.items[i].name + " and id " + result.items[i].id + " was selected in lookup");
        }
    });
    dialogwindow.show();
}


In this sample after OpenLookup function was called user will get single contact lookup form opened with all controls available like view dropdown, quick find box, new and properties button. In case you want to customize lookup window you can play with parameters from url.In case you will use this script in your Html webresource don’t forget to add reference to ClientGlobalContext.js.aspx.

I’ve tested this approach for CRM 2013 and 2015. In case it doesn’t work – that means that you’ve done something wrong or this approach is not available because it is not supported ;)