The below script will attach onchange event to a SharePoint lookup field (with items < 20 or items >= 20).
var ddlBranch;
// check if no. of items < 20 items
if($("select[title='Branch']").html() != null)
{
ddlBranch = $("select[title='Branch']")[0];
}
// check if no. of items >= 20
else if ($("input[title='Branch']").html() != null)
{
ddlBranch = $("input[title='Branch']");
// override the default HandleOptDblClick function in core.js
// added CallCustomFunction function
HandleOptDblClick = function()
{
var opt=event.srcElement;
var ctrl=document.getElementById(opt.ctrl);
SetCtrlFromOpt(ctrl, opt);
SetCtrlMatch(ctrl, opt);
opt.style.display="none";
// this checking is needed if you have other lookup fields in the form.
// this ensure that your custom function will be called only onchange of your specific lookup field
if($(ctrl).attr('title') == 'Branch')
CallCustomFunction();
}
}
// attach onchange event to ddlBranch
$(ddlBranch).change(function() {
CallCustomFunction();
});
// override the default FilterChoice function in core.js
// changed strHandler into "onclick" instead of "ondblclick"
// this will allow the user to just click once to select item instead of double click
function FilterChoice(opt, ctrl, strVal, filterVal)
{
var i;
var cOpt=0;
var bSelected=false;
var strHtml="";
var strId=opt.id;
var strName=opt.name;
var strMatch="";
var strMatchVal="";
var strOpts=ctrl.choices;
var rgopt=strOpts.split("|");
var x=AbsLeft(ctrl);
var y=AbsTop(ctrl)+ctrl.offsetHeight;
var strHidden=ctrl.optHid;
var iMac=rgopt.length - 1;
var iMatch=-1;
var unlimitedLength=false;
var strSelectedLower="";
if (opt !=null && opt.selectedIndex >=0)
{
bSelected=true;
strSelectedLower=opt.options[opt.selectedIndex].innerText;
}
for (i=0; i < rgopt.length; i=i+2)
{
var strOpt=rgopt[i];
while (i < iMac - 1 && rgopt[i+1].length==0)
{
strOpt=strOpt+"|";
i++;
if (i < iMac - 1)
{
strOpt=strOpt+rgopt[i+1];
}
i++;
}
var strValue=rgopt[i+1];
var strLowerOpt=strOpt.toLocaleLowerCase();
var strLowerVal=strVal.toLocaleLowerCase();
if (filterVal.length !=0)
bSelected=true;
if (strLowerOpt.indexOf(strLowerVal)==0)
{
var strLowerFilterVal=filterVal.toLocaleLowerCase();
if ((strLowerFilterVal.length !=0) && (strLowerOpt.indexOf(strLowerFilterVal)==0) && (strMatch.length==0))
bSelected=false;
if (strLowerOpt.length > 20)
{
unlimitedLength=true;
}
if (!bSelected || strLowerOpt==strSelectedLower)
{
strHtml+="<option selected value=\""+strValue+"\">"+STSHtmlEncode(strOpt)+"</option>";
bSelected=true;
strMatch=strOpt;
strMatchVal=strValue;
iMatch=i;
}
else
{
strHtml+="<option value=\""+strValue+"\">"+STSHtmlEncode(strOpt)+"</option>";
}
cOpt++;
}
}
var strHandler=" onclick=\"HandleOptDblClick()\" onkeydown=\"HandleOptKeyDown()\"";
var strOptHtml="";
if (unlimitedLength)
{
strOptHtml="<select tabIndex=\"-1\" ctrl=\""+ctrl.id+"\" name=\""+strName+"\" id=\""+strId+"\""+strHandler;
}
else
{
strOptHtml="<select class=\"ms-lookuptypeindropdown\" tabIndex=\"-1\" ctrl=\""+ctrl.id+"\" name=\""+strName+"\" id=\""+strId+"\""+strHandler;
}
if (cOpt==0)
{
strOptHtml+=" style=\"display:none;position:absolute;z-index:2;left:"+x+ "px;top:"+y+ "px\" onfocusout=\"OptLoseFocus(this)\"></select>";
}
else
{
strOptHtml+=" style=\"position:absolute;z-index:2;left:"+x+ "px;top:"+y+ "px\""+ " size=\""+(cOpt <=8 ? cOpt : 8)+"\""+ (cOpt==1 ? "multiple=\"true\"" : "")+ " onfocusout=\"OptLoseFocus(this)\">"+ strHtml+ "</select>";
}
opt.outerHTML=strOptHtml;
var hid=document.getElementById(strHidden);
if (iMatch !=0 || rgopt[1] !="0" )
hid.value=strMatchVal;
else
hid.value="0";
if (iMatch !=0 || rgopt[1] !="0" )
return strMatch;
else return "";
}
This site is a compilation of tutorials, code snippets, tips and tricks for SharePoint, C#, Javascript, JQuery, VB.Net, SQL and anything about programming that I have learned during my work experience.
Showing posts with label lookup field. Show all posts
Showing posts with label lookup field. Show all posts
Tuesday, 7 January 2014
Wednesday, 9 October 2013
Set Value To A Lookup Field By Using Name
The following example shows how to set value to a SharePoint lookup field by using name.
The example method or function accepts two parameters. The first one is SPList which is equal to your lookup list and the second one is a string which is equal to the name of your lookup item. This method will find the item from the lookup list by using Name. If the item is found, it will be used in creating SPFieldLookupValue category using the item id & item title. SPFieldLookupValue category will then be returned by the method and it can be used now as a value of the lookup field.
Here is the sample code on how to use the above method:
The example method or function accepts two parameters. The first one is SPList which is equal to your lookup list and the second one is a string which is equal to the name of your lookup item. This method will find the item from the lookup list by using Name. If the item is found, it will be used in creating SPFieldLookupValue category using the item id & item title. SPFieldLookupValue category will then be returned by the method and it can be used now as a value of the lookup field.
static
SPFieldLookupValue GetCategory(SPList list, string
lookupitemName)
{
SPFieldLookupValue category = null;
//get id from
Categories List
for (int ItemNum = 0; ItemNum < list.Items.Count;
ItemNum++)
{
SPListItem listItem =
list.Items[ItemNum];
if
(Convert.ToString(listItem["Title"]).Trim().ToLower() ==
lookupitemName.Trim().ToLower())
{
category = new SPFieldLookupValue(listItem.ID, listItem.Title);
break;
}
}
return
category;
}
Here is the sample code on how to use the above method:
listitem["Category"]
= GetCategory(CategoriesList, "Admin
Matters");
Subscribe to:
Posts (Atom)