Update choice field programmatically

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
October 29th, 2012 1:55pm

did you miss to update the field?

myListItem["ChoiceField"].Update();

Free Windows Admin Tool Kit Click here and download it now
October 29th, 2012 2:03pm

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
October 29th, 2012 2:06pm

Nop , AFAIK explicit field update call has to be done in order to update the field value.
Free Windows Admin Tool Kit Click here and download it now
October 29th, 2012 2:43pm

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,

October 29th, 2012 3:57pm

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();
Free Windows Admin Tool Kit Click here and download it now
October 29th, 2012 5:05pm

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.

October 30th, 2012 9:46am

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();

Free Windows Admin Tool Kit Click here and download it now
October 30th, 2012 11:13am

Thanks for "Marked As Answer"
October 30th, 2012 12:59pm

                   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??


Free Windows Admin Tool Kit Click here and download it now
December 4th, 2013 6:16am

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,

August 17th, 2015 2:47am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics