Set value of Umbraco.DropDown programatically

Hi,
Looking for some assistance in setting a Dropdown value programatically, I was under the impression for a simple Umbraco.DropDown data type I’d just be able to do the following..

ContentService c = new ContentService();
IContent myContent = c.GetById(1054);

myContent.SetValue("status", "Reserved"); // 'status' being the alias for my Umbraco.DropDown data type, 'reserved' being an available prevalue option I've set up on the data type

c.SaveAndPublishWithStatus(myContent);

A member picker, and text field which I’m also updating during the same bit of logic work without a hitch - however the dropdown doesn’t seem to work at all.

I would expect the drop down value to have changed from ‘Available’ to ‘Reserved’ however when I review the content in the backoffice the dropdown has been reset to the default (no) value.

Thought this would be quite straight forward (and apologies if I’ve missed something silly..) but starting to pull my hair out with it a little now, any help would be greatly appreciated!

Thanks


This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/54462-set-value-of-umbracodropdown-programatically

Very late to the party on this one :slight_smile: I have been stuck all day on this (Umbraco16) and finally found a very simple solution:

Say you have a drop down list called ‘Condition’ that has New and Used options. You can set it as follows:

newPage.SetValue(“condition”, “[\"New\"]”);

Or if passing a property in from an object:

newPage.SetValue(“condition”, $“["{product.Condition}"]”);

Its expecting an array with a single string entry containing the value you want to set it to, so [“New”], and then just wrap it in a string, so “[\”New\”]”.

Took me all day to fathom that one!