0

I'm creating a custom web part in asp.net 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()); 
        listItem.Update();
    }

I have tried to replace

SPListItem listItem = SPContext.Current.ListItem;

with

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.

Clarification

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.

1 Answer 1

3

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);    
    listItem.Update();   
}  
  • 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.
6
  • How do you find the ID of the selected dropdownlist? I have googled for hours with no answers. Jun 5, 2012 at 14:50
  • 1
    Can you add the code which populates ddlGroups with values to the question? Jun 5, 2012 at 14:55
  • You're fast, I've added the code. Jun 5, 2012 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. Jun 5, 2012 at 15:20
  • 1
    I've added code for the Group part Jun 5, 2012 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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