views:

31

answers:

1

Hi All,

I am busy building a custom webpart with various textboxes and lookup fields. All of them are saving correctly apart from the lookup fields that allows for multiple selections. I dont have this problem with lookup fields that only allow for one value to be selected.

Below is the code for getting all the selected items in my checkboxlist, converting it to a multichoice value and assigning to my list[columnname]

try
{
    SPFieldMultiChoiceValue _segmentchoices = new SPFieldMultiChoiceValue();
    foreach (ListItem ls3 in _segment.Items)
    {
        if (ls3.Selected) _segmentchoices.Add(ls3.Value);
    }
    myItems["Segment"] = _segmentchoices;
    myItems.Update();
}
catch (Exception ex) { _errorMessage += "||| Segment : " + ex.Message; }

The values list (_segmentchoices) is correctly created and looks like this : {;#1;#2;#3;#4;#5;#}

However when its saved it only saves values 1, 3, and 5.

My code is not generating an error, so I am at a loss at what could be wrong. Any ideas on what I need to look at? Am I going about it the wrong way?

Any assistance would be appreciated. Thank you

A: 

I just realized you are talking about a multi-select lookup field. The format should be something like: 2;#Procedures;#3;#Systems;#7;#Services

The behavior you describe makes sense because it is probably interpreting ;#1;#2;#3;#4;#5;# like this: get the item in the lookup list with a lookupID of 1 (lookupValue is 2), lookupID of 3 (lookupValue is 4), and lookupID of 5 (lookupValue is empty)

Here is some code that you can use to update a multi-select choice field:

using (SPSite site = new SPSite(siteUrl))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists[listName];
        SPListItem item = list.Items[0];

        SPFieldLookupValueCollection spflvc = new SPFieldLookupValueCollection();
        spflvc.Add(new SPFieldLookupValue(3, string.Empty));
        spflvc.Add(new SPFieldLookupValue(7, string.Empty));
        item["Keywords"] = spflvc;
        item.Update();
    }
}

The second parameter to SPFieldLookupValue doesn't seem to care if it is passed string.Empty which also might explain why it ignores them above.

Kit Menke
HI Kit, thank you for this. Learnt something new :). However I double checked and it is going in in the right order. So its still not resolving my problem. I will have a look at it again tonight and try a couple of other things and see if I can pick up what is going on.
Sonja
I just realized that you're using a multi-select lookup column. I think you need a different format because when I look in my list it shows something like: 2;#Procedures;#3;#Systems;#7;#Services
Kit Menke
Totally changed my answer... hopefully it helps.
Kit Menke
HI Kit, yes. this was it. Its now saving my values successfully. Thank you very much.
Sonja