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!
After discovering the DataTypeService, and realising that I needed the internal Id of the prevalue rather than the string value I'd assigned it.. I've put together the following solution - unsure if I've done it 'right' but works for me..
ContentService c = new ContentService();
// Fetch the content I'm updating..
IContent myContent = c.GetById(1054);
// I need to find the Id of the 'Reserved' prevalue option on the Status data type I created
DataTypeService dts = new DataTypeService();
// Get an instance of the status editor
var statusEditor = dts.GetAllDataTypeDefinitions().First(x => x.Name == "Status");
// Now we can fetch the pre-values collection, and use linq to find the Id for the 'Reserved' option
int preValueId = dts.GetPreValuesCollectionByDataTypeId(statusEditor.Id).PreValuesAsDictionary.Where(d => d.Value.Value == "Reserved").Select(f => f.Value.Id).First();
myContent.SetValue("status", preValueId); // Finally update the status with the new Id.
c.SaveAndPublishWithStatus(kit); // All done, save and publish
Hope this helps someone get on the right track - feel free to comment with amends if I've done anything daft.
Very helpful. Added this to my own helpers. Modified slightly so it will pull the Property on the Content node by the Alias, then get the data type guid from the Property.. Function for anyone that needs, saving/publishing left outside of this routine.
public static void SetDropDownValue(IContent editableNode, string propAlias, string val)
{
// Grab data type service
IDataTypeService dts = ApplicationContext.Current.Services.DataTypeService;
//find the property on the content node by its alias
Property prop = editableNode.Properties.FirstOrDefault(a => a.Alias == propAlias);
if (prop != null)
{
//get data type from the property
var dt = dts.GetDataTypeDefinitionById(prop.PropertyType.DataTypeDefinitionId);
//get the id of the prevalue for 'val'
int preValueId = dts.GetPreValuesCollectionByDataTypeId(dt.Id).PreValuesAsDictionary.Where(d => d.Value.Value == val).Select(f => f.Value.Id).First();
editableNode.SetValue(propAlias, preValueId);
}
}
Usage, in the context of the original post by Dan
ContentService c = new ContentService();
IContent myContent = c.GetById(1054);
SetDropDownValue(myContent, "status", "Reserved);
c.SaveAndPublishWithStatus(myContent);
I believe the decision to store drop down values as ID's rather than as the text value makes sense. That way, if you ever need to rename a drop down value, you only have to do that in one place (rather than on every content node that made use of that drop down).
I would not recommend replacing the built-in drop down property editor with a custom one just to change the storage mechanism.
note that once the proprty is set using the string value, you need to reset it manually through the backoffice, otherwise using the preValueId still doesn't work :(
Here are some useful methods a wrote. Sure they will come in handy....
public static int GetDataTypeId(string dataTypeName)
{
var dataTypeService = ApplicationContext.Current.Services.DataTypeService;
var allTypes = dataTypeService.GetAllDataTypeDefinitions();
return allTypes.First(x => dataTypeName.InvariantEquals(x.Name)).Id;
}
public static PreValueCollection GetPreValues(string dataTypeName)
{
int dataTypeId = GetDataTypeId(dataTypeName);
var dts = ApplicationContext.Current.Services.DataTypeService;
var preValues = dts.GetPreValuesCollectionByDataTypeId(dataTypeId);
return preValues;
}
public static List<SelectListItem> PreValuesToSelectList(string dataTypeName)
{
return (from x in GetPreValues(dataTypeName).PreValuesAsDictionary
select new SelectListItem
{
Text = x.Value.Value,
Value = x.Value.Id.ToString()
}).ToList();
}
public static string GetPreValueString(string preValueId)
{
int Id;
if(int.TryParse(preValueId, out Id))
{
return library.GetPreValueAsString(Id);
}
return string.Empty;
}
Having a similar issue on Umbraco 10 essentially if a dropdown value is not set then I want to apply a default value which seems to be fine but not seeing the update value reflected in media section unless the page is refreshed:
// If the reviewEvery property isn't set, set the default to "12 months"
SaveEventExtension.SetDropdownValues(media.Properties.FirstOrDefault(f => f.Alias == "reviewEvery"),
"Review Frequency", "12 months", _dts);
if (string.IsNullOrWhiteSpace(property?.GetValue()?.ToString()))
{
IDataType? preValueSource = dts.GetDataType(dataType);
ValueListConfiguration? preValues = (ValueListConfiguration?)preValueSource?.Configuration;
ValueListConfiguration.ValueListItem? listValue = preValues?.Items?.Find(item => item.Value == value);
property?.SetValue(
JsonConvert.SerializeObject(new[]
{
listValue.Value
})
);
}
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..
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
After discovering the DataTypeService, and realising that I needed the internal Id of the prevalue rather than the string value I'd assigned it.. I've put together the following solution - unsure if I've done it 'right' but works for me..
Hope this helps someone get on the right track - feel free to comment with amends if I've done anything daft.
Thank you Dan,
Very helpful. Added this to my own helpers. Modified slightly so it will pull the Property on the Content node by the Alias, then get the data type guid from the Property.. Function for anyone that needs, saving/publishing left outside of this routine.
Usage, in the context of the original post by Dan
This is the answer that I've been looking for! thank god someone posted it in here I can't find anything on the net.
did you find the way just by using the Name instead of the Id?
After many weeks of searching, i found this thread and this was 100% the solution i needed. Thank you so much!
Could you by any chance set your own answer as the correct one, since that makes it easier for people to find.
This is very complicated really.
What I have done is made a custom property editor for 'Dropdown' which just stores the selected value as a string.
Then I can set or retrieve the value just as a string without having to translate it.
I believe the decision to store drop down values as ID's rather than as the text value makes sense. That way, if you ever need to rename a drop down value, you only have to do that in one place (rather than on every content node that made use of that drop down).
I would not recommend replacing the built-in drop down property editor with a custom one just to change the storage mechanism.
Hello, I use a similar method has been said above but I have an error overload
Solved a problem since, but still an interesting opportunity there is another way
note that once the proprty is set using the string value, you need to reset it manually through the backoffice, otherwise using the preValueId still doesn't work :(
Hi Guys,
Here are some useful methods a wrote. Sure they will come in handy....
Hi All,
I do the same scenario in Umbraco version 8.1.0 so, How I can bind selected value in content node value and display in the back office?
var registrationContent = _contentService.Create(model.FullName, 1497, "registration"); registrationContent.SetValue("jobTitle", model.JobTitle);
I try with the above case not I can`t assign a selected value using surfacecontroller method in MVC.
Anyone have about then please give a solution so I can implement this case in my custom form.
I know it's late. But with Umbraco 8.4 I had to set the value to a Json Array of the value. Even if DDL can only select one value.....
Have only kicked the tires with U8, have you saved the new content? U7 - one of these?
Hi Guys,
For Umbraco 8 here is an example of my insert method also populating the dropdown field.
Simply wrap this around your value.
Hope this helps someone.
Regards
David
Having a similar issue on Umbraco 10 essentially if a dropdown value is not set then I want to apply a default value which seems to be fine but not seeing the update value reflected in media section unless the page is refreshed:
is working on a reply...