Umbraco: Setting A DropDownList Value Programmatically

After working on all things Hubspot over the last year whether that involved building and configuring site instances to developing API integrations, I have seemed to have missed out on CMS development-related projects. Most recently, I have been involved in an Umbraco site build where pages needed to be dynamically created via an external API.

Programmatically creating CMS pages is quite a straight-forward job as all one needs to do is:

  • Select the parent node your dynamically created page needs to reside
  • Check the parent exists
  • Create page and set field values
  • Publish

From past experience when passing values to page fields, it's been simple as passing a single value based on the field type. For example:

myNewPage.SetValue("pageTitle", "Hello World");
myNewPage.SetValue("bodyContent", "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>");
myNewPage.SetValue("hasExpired", true);
myNewPage.SetValue("price", 9.99M);

Umbraco has something completely different in mind if you plan on setting the value of type "Dropdown". Simply sending a single value will not work even though it is accepted during runtime. You will need to send a value as a Json array:

string type = "Permanent";
myNewPage.SetValue("jobType", JsonConvert.SerializeObject(new[] { type }));

This is approach is required regardless of whether you've set the "Dropdown" field type in Umbraco as single or multiple choice.


Leave A Comment

If you have any questions or suggestions, feel free to leave a comment. I do get inundated with messages regarding my posts via LinkedIn and leaving a comment below is a better place to have an open discussion. Your comment will not only help others, but also myself. :-)