Share via


Create a Lookup Button in Custom HTML Page similar to CRM Lookup

To create a lookup button in custom HTML page for that need to create a HTML web resource and then add the below mentioned required JS references in it:

<script src="../ClientGlobalContext.js.aspx" type="text/javascript"></script>
<script src="../jquery.min.js"></script>
<script src="../json2.js" type="text/javascript"></script>
<script src="../XrmServiceToolkit.js" type="text/javascript"></script>

Add the below mentioned functions in HTML page:

function getview() {
var savequeryFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"  <entity name='savedquery'>" +
"    <attribute name='name' />" +
"    <attribute name='savedqueryid' />" +
"    <filter type='and'>" +
"      <condition attribute='name' operator='eq' value='Active Accounts' />" +
"    </filter>" +
"  </entity>" +
"</fetch>";
 
var results = XrmServiceToolkit.Soap.Fetch(savequeryFetchXML);
if (results.length > 0) {
if (results[0].attributes.savedqueryid != undefined)
viewId = results[0].attributes.savedqueryid.value;
OpenLookup(viewId); 
}
}
 
function OpenLookup(defaultViewId) {
//debugger;
// prepare the crm url
var customServerURL = Xrm.Page.context.getClientUrl();
var objectCode = "1";
var url = customServerURL + "/_controls/lookup/lookupsingle.aspx?class=null&objecttypes=" + objectCode + "&browse=0&ShowNewButton=0&ShowPropButton=1&DefaultType=1&DefaultViewId=%7b" + defaultViewId + "%7d ";
var lookUp = openStdDlg(url, "", 600, 600, false);
if (lookUp != null) {
alert(lookUp.items[0].id);// get Id. 
alert(lookUp.items[0].name);//get Name. 
}
}

If you want to open any specific view on click of the lookup button then you can use the getview() function and here you can pass the view name in FetchQuery as a filter condition to get the view Id.

Now, we can call the getview() function OnClick() event of HTML button to achieve the desired result.