locked
Update choice field programmatically RRS feed

  • Question

  • Hi,

    How to update a choice field?

    I have a choice field(dropdown) in my custom list, which has the value as "Start", "In Progress", "End".

    I would like to update the list items which have the value as "In Progress" to "End" programmatically.

    Is the below code correct. I have not got any errors, however would like to know if the below is proper. I have not provided the condition in the below code, however the update code i have added

    myListItem["ChoiceField"] = myList.Fields["ChoiceField"].GetFieldValue("End");
    myListItem.Update();
    myList.Update();

    Thank you


    • Edited by Venkatzeus Monday, October 29, 2012 1:56 PM added more info
    Monday, October 29, 2012 1:55 PM

Answers

  • Hello,

    actually with choice field it is enough to state text value you wish to set (unlike lookup field)

    This works for me

    SPListItem item = ...
    item["ChoiceField"] = "End";
    item.Update();

    Hope this helps,


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    • Proposed as answer by Borislav Grgić Tuesday, October 30, 2012 11:35 AM
    • Marked as answer by Venkatzeus Tuesday, October 30, 2012 11:44 AM
    Monday, October 29, 2012 3:57 PM
  • Hi,

     Venkat- Why don't you do like  just set it as a string. That's how the values of choice fields are stored internally.

     Just look at the below sample code snippet.

    SPListItem item = ...
    item["ChoiceFieldInternalName"] = "Your value";
    item.Update();

    • Proposed as answer by SekThang Tuesday, October 30, 2012 11:13 AM
    • Marked as answer by Venkatzeus Tuesday, October 30, 2012 11:44 AM
    Tuesday, October 30, 2012 11:13 AM

All replies

  • did you miss to update the field?

    myListItem["ChoiceField"].Update();


    Bhushan | http://www.passionatetechie.blogspot.com | http://twitter.com/BhushanGawale

    Monday, October 29, 2012 2:03 PM
  • HI,

    Thank you very much for the reply. But is this not fine:

    myListItem.Update();

    Also, is the first line correct?

    Thanks


    • Edited by Venkatzeus Monday, October 29, 2012 2:11 PM
    Monday, October 29, 2012 2:06 PM
  • Nop , AFAIK explicit field update call has to be done in order to update the field value.

    Bhushan | http://www.passionatetechie.blogspot.com | http://twitter.com/BhushanGawale

    Monday, October 29, 2012 2:43 PM
  • Hello,

    actually with choice field it is enough to state text value you wish to set (unlike lookup field)

    This works for me

    SPListItem item = ...
    item["ChoiceField"] = "End";
    item.Update();

    Hope this helps,


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    • Proposed as answer by Borislav Grgić Tuesday, October 30, 2012 11:35 AM
    • Marked as answer by Venkatzeus Tuesday, October 30, 2012 11:44 AM
    Monday, October 29, 2012 3:57 PM
  • Hi Venkat,

     Please try to look at the below example code snippet. It may help you to accomplish your task,

     
    /* get the newly added choice field instance */
    SPFieldChoice chFldGender = (SPFieldChoice)lst.Fields["Gender"];
     
    /* set field format type i.e. radio / dropdown */
    chFldGender.EditFormat = SPChoiceFormatType.Dropdown;
     
    /* set the choice strings and update the field */
    chFldGender.Choices.Add("Male");
    chFldGender.Choices.Add("Female");
    chFldGender.Update();
     
    /* finally update list */
    lst.Update();
    • Proposed as answer by SekThang Monday, October 29, 2012 5:06 PM
    • Unproposed as answer by Venkatzeus Tuesday, October 30, 2012 5:55 AM
    • Proposed as answer by SekThang Tuesday, October 30, 2012 7:08 AM
    • Unproposed as answer by Venkatzeus Tuesday, October 30, 2012 9:44 AM
    • Proposed as answer by navathehutt Monday, July 14, 2014 12:34 AM
    Monday, October 29, 2012 5:05 PM
  • Hi,

    @SekThang - I am unproposing the answer, as i am not adding new records to the choice field.

    I am trying to update the choice field.

    Tuesday, October 30, 2012 9:46 AM
  • Hi,

     Venkat- Why don't you do like  just set it as a string. That's how the values of choice fields are stored internally.

     Just look at the below sample code snippet.

    SPListItem item = ...
    item["ChoiceFieldInternalName"] = "Your value";
    item.Update();

    • Proposed as answer by SekThang Tuesday, October 30, 2012 11:13 AM
    • Marked as answer by Venkatzeus Tuesday, October 30, 2012 11:44 AM
    Tuesday, October 30, 2012 11:13 AM
  • Thanks for "Marked As Answer"
    • Proposed as answer by SaMolPP Monday, July 31, 2017 3:20 PM
    Tuesday, October 30, 2012 12:59 PM
  •                    object folderTitle;
                        if (properties.TryGetValue("Title", out folderTitle))
                            newItem["Title"] = folderTitle;
                        foreach (var kvp in newItemProperties)
                        {
                            newItem[kvp.Key] = kvp.Value;
                        }
    		    newItem.Update();
                        clientContext.Load(newItem);
                        clientContext.ExecuteQuery();

    I have list of the properties on the newItemproperties and then i run the following code it gives me error as :Item does not exist. It may have been deleted by another user.

    I have inner exception as: 

     at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
       at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
       at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
       at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
       at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
       at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
    

    Why is am i getting this error??
    Is there anything i have missed to do??


    Wednesday, December 4, 2013 6:16 AM
  • Hello,

    actually with choice field it is enough to state text value you wish to set (unlike lookup field)

    This works for me

    SPListItem item = ...
    item["ChoiceField"] = "End";
    item.Update();

    Hope this helps,


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.


    Monday, August 17, 2015 6:45 AM