Friday, September 18, 2009

Select All option for Lookup fields

Scenario:
Lookup with multi-select option is available as a field for any list we create. And it has Add and Remove buttons to make selection but misses Add All and Remove All.

Solution:
This is a small workaround. Add a check-box column to the list as shown below.
Now you need to find some control guids. You can use my favorite X-ray machine Internet Explorer Developer Toolbar.

Steps:
1. Enter into edit mode of the NewForm.aspx ( type javascript:MSOLayout_ToggleLayoutMode(); in the IE Address bar and hit enter )

2. Add a Content Editor Webpart at the bottom ( make sure it is the last webpart )

3. Paste the script given below ( make sure to change the control ids )
selectall

Code:

 <script type="text/javascript">

function button_click(controlid,AddButtonControlId)
{
var myselect = document.getElementById(controlid)

for (var i = 0; i < myselect.options.length; i++)
{
myselect.options[i].selected = true ;
}

for (var i = 0; i < myselect.options.length; i++)
{
if (myselect.options[i].selected == true)
{
GipAddSelectedItems(AddButtonControlId);

}
}
}

function SelectAll()
{

// passed paramters
// 1. select control ( must be in quotes ) - ends in SelectCandidate
// 2. Picker control id ( must not have quotes ) - ends in MultiLookupPicker_m
button_click("ctl00_m_g_dbc86119_02b0_465b_8e56_2764e7284a1d_ctl00_ctl04_ctl01_ctl00_ctl00_ctl04_ctl00_ctl00_SelectCandidate",ctl00_m_g_dbc86119_02b0_465b_8e56_2764e7284a1d_ctl00_ctl04_ctl01_ctl00_ctl00_ctl04_ctl00_ctl00_MultiLookupPicker_m) ;
}

// passed paramters
// 1. check box control ( must be in quotes )

var checkit = document.getElementById("ctl00_m_g_dbc86119_02b0_465b_8e56_2764e7284a1d_ctl00_ctl04_ctl03_ctl00_ctl00_ctl04_ctl00_ctl00");
checkit.onclick = SelectAll;

</script>

4 comments:

rgtheprogrammer November 21, 2011 at 3:10 PM  

Hi Sandeep,
I am trying to update a multivalue lookup field from a web part which contains check boxes. In this case, the multivalue field will always contain all of the "candidate" values. So, whichever values are selected in the checkboxes, these values will populate in the multivalue lookup field. This means that the candidate and possibly the result values will need to be cleared every time. Is there a way to do this? Also, where can I find decent documentation on the "gip" functions available in Javascript? For exmaple, gipaddselecteditems.

rgtheprogrammer November 21, 2011 at 3:15 PM  

Hello Sandeep,
I am trying to populate a multivalued lookup field from a web part on the same web page. The multivalued lookup field will always reflect the choices from the web part. So I am thinking that all I need is to populate the "SelectCandidate" part of the field from the web part and then use your code to move from selected to result. Is there a way to do this? Also, where can I find decent documentation on the "gip" functions available in Javascript? For example, gipaddselecteditems.

Sandeep November 22, 2011 at 3:09 AM  

not sure :(
I usually write own functions in Jquery

rgtheprogrammer November 22, 2011 at 11:35 AM  

How does the GipAddSelectedItems function work? I'm thinking the argument is the control you are writing to. Can the selected items come from any control outside of the control you are writing to?