the short answer is not easily, and the second answer is I would try not to if i was you :)
but the third answer ....
IPublishedContent is getting your content from the cache and this is loosly coupled from the data, so while you can tell if it's say and INT or a string, its much harder to work out if it's something more special. (you can cast properties types and use the Core Property Value converter package but you do have to know what they
are)
if you really need to know what the underling datatype is then you need to go to the back office api which will hit the database and will be slow!!
One way:
get the doctype from the published item,
get the property from the doctype
get the datatype from the property
.
var docTypeAlias = Model.Content.DocumentTypeAlias;
var docType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(docTypeAlias);
var property = docType.PropertyTypes.Where(x => x.Alias == "propertyAlias").FirstOrDefault();
if (property != null)
{
var datatype = ApplicationContext.Current.Services.DataTypeService
.GetDataTypeDefinitionById(property.DataTypeDefinitionId);
var name = datatype.Name;
}
Second way,
get the Content item from the published content
get the property from the content
use that to get the data type
.
var content = ApplicationContext.Current.Services.ContentService.GetById(Model.Content.Id);
if (content.HasProperty("propertyName"))
{
var property = content.Properties.Where(x => x.Alias == "propertyAlias").FirstOrDefault();
if (property != null)
{
....
}
}
but neither of these is really right, they will be slow, and hit the database a bit.
if it's certain type you are looking for you might just be better sniffing for it, based on the structure so if you look in the app_data/umbraco.config file (which is the cache, don't lock it or edit it!). you might see that the type you are looking for is JSON or has a certain format you can just detect in the string.
Great answers - They got me to the right place and stuff is working :)
My need was to duplicate "Umbraco.DropDown" on an angular front-end. For anybody interested, this is how i got there:
public class UmbracoDropDown
{
public int Id { get; set; }
public string Alias { get; set; }
public string Value { get; set; }
public List<string> Options { get; set; } = new List<string>();
}
public UmbracoDropDown GetDropDown(int contentId, string propertyName)
{
var content = _contentService.GetById(contentId);
var dropDown = content?.Properties.First(x => x.PropertyType.PropertyEditorAlias == "Umbraco.DropDown" && x.Alias.InvariantEquals(propertyName));
if (dropDown == null)
return null;
var model = new UmbracoDropDown()
{
Id = dropDown.Id,
Alias = dropDown.Alias,
Value = _dataTypeService.GetPreValueAsString(content.GetValue<int>(dropDown.PropertyType.Alias)),
Options = _dataTypeService.GetPreValuesByDataTypeId(dropDown.PropertyType.DataTypeDefinitionId).ToList()
};
return model;
}
IPublishedContent property datatype
Hi,
Is it possible to get an IPublishedContent property to tell me what datatype it has assigned for itself?
Ps. I did try to ask it but no answer.
Thanks,
//casper
Hi Casper
the short answer is not easily, and the second answer is I would try not to if i was you :)
but the third answer ....
IPublishedContent is getting your content from the cache and this is loosly coupled from the data, so while you can tell if it's say and INT or a string, its much harder to work out if it's something more special. (you can cast properties types and use the Core Property Value converter package but you do have to know what they are)
if you really need to know what the underling datatype is then you need to go to the back office api which will hit the database and will be slow!!
One way:
.
Second way,
.
but neither of these is really right, they will be slow, and hit the database a bit.
if it's certain type you are looking for you might just be better sniffing for it, based on the structure so if you look in the app_data/umbraco.config file (which is the cache, don't lock it or edit it!). you might see that the type you are looking for is JSON or has a certain format you can just detect in the string.
Hi Kevin,
Great answers - They got me to the right place and stuff is working :)
My need was to duplicate "Umbraco.DropDown" on an angular front-end. For anybody interested, this is how i got there:
The end result being something like this:
Thanks man!
is working on a reply...