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

I have following code (didn't list the whole code) behind a button on a InfoPath Managed Form. and it works meaning I can submit data to a list. Now I have 2 controls on the form that is Multiple-Selection List Box (Data source is a sharepoint list). The Target column (Lookup type with Allow Multiple values) in the target list is "Counties". Both Infopath and target sharepoint list gets information from a list called "City". How would I code

item["Counties"] = need help here;


SPListItem item = list.Items.Add();
item["Title"] = MainDataSource.CreateNavigator().SelectSingleNode("//my:fldTitle", NamespaceManager).Value;

Updated: Found this piece. I guess I will still need to find out the XML way to extract id and it's value from xml tree of Multiple selection List.

SPListItem item = SPContext.Current.List.GetItemById(1);
SPFieldLookupValueCollection values = new SPFieldLookupValueCollection();
values.Add(new SPFieldLookupValue(1,string.Empty));
values.Add(new SPFieldLookupValue(3,string.Empty));
item["LookupField"] = values;
item.Update();

I found another cool link. I think I am good now. http://www.bizsupportonline.net/blog/2009/03/programmatically-retrieve-selected-items-multiple-selection-list-box-infopath/

share|improve this question
    
Here is a good example what I am doing. Except I have additional Multiple-selection items (or lookup column) . bizsupportonline.net/browserforms/… –  Joe Yee Sep 7 '12 at 17:14

1 Answer 1

Ha! I notice that code block is from my blog article, so I don't feel too guilty about duplicating it here as an answer:

SPListItem item = SPContext.Current.List.GetItemById(1);
SPFieldLookupValueCollection values = new SPFieldLookupValueCollection();
values.Add(new SPFieldLookupValue(1,string.Empty));
values.Add(new SPFieldLookupValue(3,string.Empty));
item["LookupField"] = values;
item.Update();

Along with the code to set a single-value lookup as a bonus:

SPListItem item = SPContext.Current.List.GetItemById(1);
int id = 1;
item["LookupField"] = new SPFieldLookupValue(id, string.Empty);
item.Update();

Here's the original post: Easily accessing Lookup and User fields

share|improve this answer

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.