locked
Save user's selection from a lookup list field RRS feed

  • Question

  • I've figured out how to add a record into a list. The only thing I am trying to figure out is how (or maybe where) do I save a user's selection from a lookup list?

    In the below code snippet, I am saving a new list item. It saves without error, but the fields "AwardType" and "AwardReason" are lookup fields and, although I do not get an error, nothing gets saved to them. How do I save to a lookup field?

    using (SPSite site = new SPSite(SPContext.Current.Web.Url))
    {
    	using (SPWeb web = site.OpenWeb())
    	{
    		using (FileStream fs = (new FileInfo(fileUpload.PostedFile.FileName)).OpenRead())
    		{
    			SPList list = web.Lists["Awards"];
    			Hashtable ht = new Hashtable();
    			ht.Add("wfRecipientName", txtRecipientName.Text);
    			ht.Add("Office", txtOrganization.Value);
    			ht.Add("AwardType", ddAwardTypes.SelectedValue);
    			ht.Add("AwardReason", ddAwardReasons.SelectedValue);
    
    			SPFile destfile = list.RootFolder.Files.Add(fileUpload.FileName, fs, ht, false);
    		}
    	}
    }

    • Moved by Entan Ming Friday, September 7, 2012 9:50 AM programming issue (From:SharePoint 2010 - Using SharePoint Designer, Infopath, and other customization)
    Thursday, September 6, 2012 6:57 PM

Answers

  • Hi Wannabe67,

    The string value of look up field is looks like this: (ID);#(Value). So ui suggest you try the following code:

    ht.Add("AwardType","1;#"+ ddAwardTypes.SelectedValue);//1 is corresponding the value in the lookup list
    ht.Add("AwardReason","1;#" ddAwardReasons.SelectedValue);
    //

    Thanks,
    Lhan Han



    • Edited by Lhan Han Wednesday, September 12, 2012 8:33 AM
    • Marked as answer by Lhan Han Friday, September 14, 2012 12:35 PM
    Wednesday, September 12, 2012 8:33 AM