Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

On http://weblogs.asp.net/bsimser/archive/2013/02/20/defaulting-values-in-a-multi-lookup-form-in-sharepoint.aspx I found some nice code to pre-populate a multilookuplist (and remove the selected items).

In my case I want to do this dynamically but the "removal" line does not work, probably my jquery... what should it be? (the rest works fine)

My code:

        if (keyOrClick === 'click') {               
            dataValues = $(":input[id$='MultiLookupPicker_data']").val();
            arrDataValues = GipSplit(dataValues);
            index = $.inArray(contractNrEntered, arrDataValues);
            var $resultOptions = "<OPTION title=\"" + arrDataValues[index] + "\" value=\"" + arrDataValues[index-1] + "\">" 
                + arrDataValues[index] + "</OPTION";
            var $resultSpOptions = arrDataValues[index-1] + "|t" + arrDataValues[index];
            var possibleValues = $("[id$='_SelectCandidate']");

            // @todo next line does not remove the option
            $("[id$='_SelectCandidate']").find("option[value='"+ arrDataValues[index] +"']").remove();

            possibleValues.val(1);
            $("[id$='_SelectResult']").append($resultOptions);
            $("[id$='MultiLookupPicker']").val($resultSpOptions); 
        }

UPDATE/Solution

As giveback to readers: I wanted a textbox above the multilookup box that a) adds items when user selects "add" button" b) inserts items in lookup list when user selects "insert" button to make it work seamlessly. Since the code is longer than to be pasted here: I posted the complete code here: http://edward.de.leau.net/sharepoint-lookup-add-insert-field-multi-select-lookup-column-20130608.html if you ever need this.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I'm not sure why your code doesn't work - it's a little hard to follow but I suspect the hidden input is not being set properly. I wrote up a couple of functions to add and remove selections, and made an option to work in either 2010 or 2013. I think you'll find them simpler to understand and use that what you have there.

The text boxes with the selections, like many items in SharePoint forms, use the "title" attribute to indicate which field they belong to. Consider the following screen shot:

enter image description here

The multi-select column is called "Project Document", so the text boxes have title attributes of "Project Document possible values" and "Project Document selected values". We'll reference these attributes rather than looking for the ending of the ID when we set the visible options.

For the hidden input, I still needed to use the ID's ending, but I noticed that in 2013 the values are a little different. In 2010 the selector is "[id$='MultiLookupPicker']" while in 2013 the selector is "[id$='MultiLookup']".

With that said here are the functions. The process to add and remove the items works a little differently based how many items are in each of the text boxes in some cases, that's what the if statements handle. Note the selector variable and uncomment the appropriate one for 2010 or 2013 depending on your deployment target. Also note I hard-coded the column name, "Project Document". You'll need to change that for your column.

//var selector = "[id$='MultiLookupPicker']"; //for 2010
var selector = "[id$='MultiLookup']"; //for 2013

  function addChoice(text) {
    $("[title='Project Document possible values'] option").each(function () {
        if ($(this).text() == text) {
            $(this).appendTo($("[title='Project Document selected values']"));
            var multilookupPickerVal = $(selector).val();
            if ($(selector).val() == undefined || $(selector).val().length == 0) {
                $(selector).val($(this).val() + "|t" + $(this).text());
            }
            else {
                $(selector).val(multilookupPickerVal + "|t" + $(this).val() + "|t" + $(this).text());
            }
        }
    });
}

function removeChoice(text) {
    $("[title='Project Document selected values'] option").each(function () {
        if ($(this).text() == text) {
            $(this).appendTo($("[title='Project Document possible values']"));
            var multilookupPickerVal = $(selector).val();
            var valToRemove = $(this).val() + "|t" + $(this).text();
            var newValue = multilookupPickerVal.replace(valToRemove, "");

            $(selector).val(newValue);
        }
    });
}
share|improve this answer
1  
sorry about the indenting. I can't seem to make it format properly here. –  Derek Gusoff Jun 6 '13 at 3:59
    
Ah, you use an "each" with an replace. Another approach but this works. Nice! Thanks. –  edelwater Jun 8 '13 at 0:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.