SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm creating a custom web part in for SharePoint 2010 programmatically. When a user presses an Add button, the selected item from the dropdownlist is supposed to then be added to the Groups field of the current list. The Groups field is a lookup type from another list. Here is the code from the button event:

    protected void ButtonGroup_Click(object sender, EventArgs e)
        DeleteGroupButton1.Visible = true;
        GroupLabel1.Text = ddlGroups.SelectedValue.ToString();

        SPSite site = SPContext.Current.Site;
        SPWeb web = SPContext.Current.Web;
        SPList list = web.Lists["Contacts"];

        SPListItem listItem = SPContext.Current.ListItem;
        listItem["Groups"] = new SPFieldLookupValue(SPContext.Current.ItemId, ddlGroups.SelectedValue.ToString()); 

I have tried to replace

SPListItem listItem = SPContext.Current.ListItem;


SPListItemCollection items = list.Items;

SPListItem listItem = items[0];

and it added the correct field from the lookup for the first item. Using an index is not desirable. In addition, the group is never updated when I go to edit it.


I have two Lists. One is Contacts and the other is Groups. I have a Lookup column in Contacts called Groups that points to the List Groups.

Added dropdownlist population

            SPList groupList = web.Lists["Groups"];
            SPQuery groupQuery = new SPQuery();
            groupQuery.Query = "<Query><OrderBy><FieldRef Name='Title'/></OrderBy></Query>";
            SPListItemCollection groupListItemCol = groupList.GetItems(groupQuery);

            foreach (SPListItem groupListItem in groupListItemCol)
                ddlGroups.Items.Add(new ListItem(groupListItem["Title"].ToString())); //Adding Title column one by one

It's nested and I have omitted the rest of the code. It does populate the dropdown.

share|improve this question
up vote 3 down vote accepted

A couple of issues:

  • The Id part of SPFieldLookupValue should be the Id of the listitem in the list with groups (Groups?) not the Id of the item you're updating. So ideally you setup the ddlGroups with Text=Text from Groups and Value=Id of corresponding listitem in Groups.

The code to populate the ddlGroups should be changed to:

foreach (SPListItem groupListItem in groupListItemCol) 
  ddlGroups.Items.Add(new ListItem(groupListItem.Title, groupListItem.ID.ToString())); //Adding Title column one by one 

the button code can then be something like:

protected void ButtonGroup_Click(object sender, EventArgs e)   
    DeleteGroupButton1.Visible = true;
    var selectedGroup = ddlGroups.SelectedItem;
    GroupLabel1.Text = selectedGroup.Text;

    // Get listitem somehow

    listItem["Groups"] = new SPFieldLookupValue(int.Parse(selectedGroup.Value), selectedGroup.Text);    
  • You need to find out how to determine which listitem in Contacts is going to be updated. Using SPContext.Current requires that the URL contains information to find the list as well as an ID query parameter specifying the ID of the listitem.
share|improve this answer
How do you find the ID of the selected dropdownlist? I have googled for hours with no answers. – MiniRagnarok Jun 5 '12 at 14:50
Can you add the code which populates ddlGroups with values to the question? – Per Jakobsen Jun 5 '12 at 14:55
You're fast, I've added the code. – MiniRagnarok Jun 5 '12 at 14:59
I was thinking that the id must be the 1st number from "1;#Group1". Which is the id of the listItem that I'm working with. – MiniRagnarok Jun 5 '12 at 15:20
I've added code for the Group part – Per Jakobsen Jun 5 '12 at 16:06

Your Answer


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.