I have often seen people messing up with the lookup in their custom add ons to MSCRM 3.0
I made a custom lookup control for the same problem. its really simple and working perfectly fine for all entities
it has total 5 parts that you need to look for
1. ascx (lookup control ) file
2. javascript3. Codebehind (.cs) file
4. Style sheet
5. Page on which lookup control resides
let me explain all of them in order
1. ASCX (look and feel of lookup)
<%@ Control Language=”C#” AutoEventWireup=”true” CodeFile=”usrCtlLookUp.ascx.cs” Inherits=”usercontrols_usrCtlLookUp” %><table runat=”server” id=”tblMain” width=”84%” cellpadding=”0″ cellspacing=”0″ border=”0″>
<tr>
<td>
<div class=”lookup” id=”LookupText” runat=”server”></div>
</td>
<td style=”width:2px”></td>
<td style=”width:21″>
<img alt=”look up” src=”../images/lookup.gif” runat=”server” id=”imgLookup”/>
</td>
</tr>
</table><input type=”hidden” runat=”server” id=”hdnEntityId” />
<input type=”hidden” runat=”server” id=”hdnEntityName” />
2. Javascript file
// JScript Filefunction LookupArgsClass(){this.items = null;}function SetRegardingValues(entityCode,divId,hdnId){
var serverURL = document.getElementById(“hdnServerName”).value;var sPath= serverURL + ‘/_controls/lookup/lookupsingle.aspx?class=BasicOwner&objecttypes=’+entityCode+‘&browse=0&DefaultType=0′ var left = (screen.width/4);var top = (screen.height/5); if(entityCode == “1″ || entityCode == “2″ || entityCode == “10012″){//sPath= serverURL + ‘/_controls/lookup/lookupsingle.aspx?class=null&objecttypes=1&browse=0&DefaultType=0′ sPath= serverURL + ‘/_controls/lookup/lookupsingle.aspx?class=null&objecttypes=’+entityCode+‘&browse=0&DefaultType=0′ }
else if(entityCode == 8){sPath= serverURL + ‘/_controls/lookup/lookupsingle.aspx?class=BasicOwner&objecttypes=8&browse=0&DefaultType=0′ }var args = new LookupArgsClass(); args.items = document.getElementsByTagName(“SPAN”);var sCustomWinParams = “dialogWidth:600px0px;dialogHeight:488px;dialogLeft=”+left+“px;dialogTop=”+top+“px;help:0;status:1;scroll:0;center:1;resizable:yes;”var objlookup = window.showModalDialog(sPath,args,sCustomWinParams); if(objlookup != null){document.getElementById(hdnId.id).value = objlookup.items[0].id;document.getElementById(divId.id).innerHTML =
“ <a href=’#'>” + objlookup.items[0].name + “</a>”; }}function OpenUserEditDialogue(entityCode,divId,hdnId){
if(document.getElementById(hdnId.id).value == “”){return false;}var serverURL = document.getElementById(“hdnServerName”).value; var left = (screen.width/4);var top = (screen.height/5);var sPath= serverURL+ “/sfa/accts/edit.aspx?id=” + document.getElementById(hdnId.id).value;var sCustomWinParams = “dialogWidth:930px0px;dialogHeight:675px;dialogLeft=”+left+“px;dialogTop=”+top+“px;help:0;status:1;scroll:0;center:1;resizable:yes;”if(entityCode == “10001″){sPath= serverURL + ‘/_controls/lookup/lookupsingle.aspx?class=BasicOwner&objecttypes=’+entityCode+‘&browse=0&DefaultType=0′sCustomWinParams = “dialogWidth:600px0px;dialogHeight:488px;dialogLeft=”+left+“px;dialogTop=”+top+“px;help:0;status:1;scroll:0;center:1;resizable:yes;”
}if(entityCode == “8″){sPath= serverURL + ‘/_controls/lookup/lookupsingle.aspx?class=BasicOwner&objecttypes=8&browse=0&DefaultType=0′sCustomWinParams = “dialogWidth:831px0px;dialogHeight:641px;dialogLeft=”+left+“px;dialogTop=”+top+“px;help:0;status:1;scroll:0;center:1;resizable:yes;”
}else if(entityCode == “10012″){spath = serverURL + “_controls/lookup/lookupsingle.aspx?class=null&objecttypes=10012&browse=0&DefaultType=0″sCustomWinParams = “dialogWidth:1012px;dialogHeight:642px;dialogLeft=”+left+“px;dialogTop=”+top+“px;help:0;status:1;scroll:0;center:1;resizable:yes;”
}else if(entityCode == “2″){spath = serverURL +
“_controls/lookup/lookupsingle.aspx?class=null&objecttypes=2&browse=0&DefaultType=0″ }var args = new LookupArgsClass(); var objlookup = window.showModalDialog(sPath,args,sCustomWinParams);if(objlookup != null){document.getElementById(hdnId.id).value = objlookup.items[0].id;document.getElementById(divId.id).innerHTML = “ <a href=’#'>” + objlookup.items[0].name + “</a>”;}}
function CloseWindow(){window.close();return false;}
function ChangeStyle(divId,mouseOver){if(mouseOver){document.getElementById(divId.id).className = “lookupHover”;}else
{document.getElementById(divId.id).className = “lookup”;}}3. CodeBehind file
using System;using System.Data;using System.Configuration;using System.Collections;
using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Reflection;using CRMUtils.CrmSdk;
using CRMBusinessEntities;using CRMBusinessEntities.Operations;public partial class usercontrols_usrCtlLookUp : System.Web.UI.UserControl,IWebUserControl{public void LoadUserControl(Guid id){
if (id == Guid.Empty){id = new Guid(LookupEntityID);}else {LookupEntityID = id.ToString();}
string name = string.Empty;if(LookupEntityName.Equals(EntityName.account.ToString())){CRMAccount objAccount = new CRMAccount();account accountBe = (account)objAccount.GetAccountById(id,
new string[] { “accountid”, “name” });name = accountBe.name;}else if (LookupEntityName.Equals(EntityName.systemuser.ToString())){CRMUser objSystemUser = new CRMUser();systemuser systemuserBe = (systemuser)objSystemUser.GetSystemUserById(id, new string[] { “systemuserid”, “fullname” });name = systemuserBe.fullname;}else if (LookupEntityName.Equals(EntityName.new_hotel.ToString())){CRMHotel objHotel =
new CRMHotel();new_hotel hotelBe = (new_hotel)objHotel.GetHotelByHotelID(id, new string[] { “new_hotelid”, “new_hotelname” });name = hotelBe.new_hotelname;}
else if (LookupEntityName.Equals(EntityName.contact.ToString())){CRMContact objContact = new CRMContact();contact contactBe = (contact)objContact.GetContactById(id, new string[] { “contactid”, “fullname” });name = contactBe.fullname;}this.LookupText.InnerHtml = “ <a href=’#'>” + name + “</a>”;}
public void UpdateUserControl(){}protected void Page_Load(object sender, EventArgs e){CRMMetaData metaData = new CRMMetaData();this.imgLookup.Attributes.Add(“onclick”, “return SetRegardingValues(“ + metaData.GetEnityTypeCode(LookupEntityName) + “,” + this.LookupText.ClientID.ToString() + “,” + this.hdnEntityId.ClientID.ToString() + “);”);
this.LookupText.Attributes.Add(“onclick”, “return OpenUserEditDialogue(“ + metaData.GetEnityTypeCode(LookupEntityName) + “,” + this.LookupText.ClientID.ToString() + “,” + this.hdnEntityId.ClientID.ToString() + “);”);this.LookupText.Attributes.Add(“onmouseover”, “return ChangeStyle(“+ this.LookupText.ClientID.ToString() + “,true);”);this.LookupText.Attributes.Add(“onmouseover”, “return ChangeStyle(“ + this.LookupText.ClientID.ToString() + “,false);”); }public string LookupEntityID{get {return hdnEntityId.Value;}set
{hdnEntityId.Value = value;if (value == string.Empty){LookupText.InnerHtml = “”;}}}
public string LookupEntityName{get
{return hdnEntityName.Value;}set{hdnEntityName.Value = value;}}}
4. CSS File (style sheet)
.lookup{
border-right: #7b9ebd 1px solid; border-top: #7b9ebd 1px solid; border-left: #7b9ebd 1px solid; width: 97%; border-bottom: #7b9ebd 1px solid; height: 18px; background-color:White;
}
.lookupHover{
border-right: #7b9ebd 1px solid; border-top: #7b9ebd 1px solid; border-left: #7b9ebd 1px solid; width: 97%; border-bottom: #7b9ebd 1px solid; height: 18px; background-color:#ADC3E7;
}
5. Page or control (on which lookup control resides)
protected void Page_Load(object sender, EventArgs e)
{
//just set the entity name for the lookup control
uclookUpControl.LookupEntityName = EntityName.account.ToString();
}
/*call the following function from anywhere in the container control (e.g on which you have draged and dropd the lookup control) to get the entity name populated, do not pass guid.Empty, but pass the entity ID here so that it could load the entity name, if you pass it empty, then you have to set the “LookupEntityID” property of the lookup control to the entity ID you want it to populate with */
uclookUpControl.LoadUserControl(Guid.Empty);
======================================================================
if you feel anything unclear, you can always send me an email at irfanyar@gmail.com
===========================================================