v6/7 API equivalent for uQuery.GetUmbracoObjectType
Hi All,
I have been working on a value converter for the v7 multinode tree picker, this value converter doesn't know if the picker is set to pick "content" or "media" but it needs to know.
I have previously used (and can still use) a really useful uQuery method called GetUmbracoObjectType which makes this really simple but I'm wondering if there is a current API method that does the same or if not should there be one?
if (uQuery.GetUmbracoObjectType(1234) == uQuery.UmbracoObjectType.Document)
An alternative approach is to look at the prevalue setting itself, but this is a lot more complicated, here's how it looks:
var dts = ApplicationContext.Current.Services.DataTypeService;
var startNodePreValue =
dts.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeId)
.PreValuesAsDictionary.FirstOrDefault(x => x.Key.ToLowerInvariant() == "startNode".ToLowerInvariant()).Value.Value;
var startNodeObj = JsonConvert.DeserializeObject<JObject>(startNodePreValue);
var pickerType = startNodeObj.GetValue("type").Value<string>();
Looks totally perfect, however I'm getting an exception when passing in a node id?
var objectType = ApplicationContext.Current.Services.EntityService.GetObjectType(1068);
Exception:
System.InvalidCastException was unhandled by user code
HResult=-2147467262
Message=Object must implement IConvertible.
Source=mscorlib
StackTrace:
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType)
at Umbraco.Core.Persistence.Database.ExecuteScalar[T](String sql, Object[] args)
at Umbraco.Core.Persistence.Database.ExecuteScalar[T](Sql sql)
at Umbraco.Core.Services.EntityService.GetObjectType(Int32 id)
at OurUmbraco.PropertyConverters.MultiNodeTreePickerPropertyConverter.ConvertDataToSource(PublishedPropertyType propertyType, Object source, Boolean preview) in c:\Users\Jeavon\Documents\Visual Studio Projects\BitBucket Umbraco-Core-Property-Editor-Converters\OurUmbraco.PropertyConverters\MultiNodeTreePickerPropertyConverter.cs:line 48
at Umbraco.Core.Models.PublishedContent.PublishedPropertyType.ConvertDataToSource(Object source, Boolean preview)
at Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedProperty.<.ctor>b__0()
at System.Lazy`1.CreateValue()
InnerException:
Ah, its doing an ExecuteScalar<string> on nodeObjectType which is actually a nullable Guid. So that's probably where the conversion issue is happening :-S
I've got a other problem that maybe someone here know. If building a object generator and i need to find out if an object is a string or an other object. I've got the DataType like this:
Did not know that Jeavon! Thanks, But because i got my own ORM (with save and dependency injection) i need to write my own that works with that ORM. But i can look at his code how he finds the type. Thanks!
Jeavon, maybe you know this. I'm trying to find the ClrType from a object. This is what is got:
Type type = Umbraco.Core.Models.PublishedContent.PublishedContentType.Get(publishedItemType, contentType.Alias).GetPropertyType(propertyType.Alias).ClrType;
But if i look at the mediatype image, umbracowidth it says it's a object. But in a debugger it says it is a Int? Where can i find that ClrType that is that Int?
I'm afraid I don't but IPublishedContentModelFactory is not part of Stephan's model generator, it is a hook built into the Umbraco v7.1.4 Core for creating projects such as this. I believe it may expose the types of all the properties in the installation (taking into account value converters) as I think I remember Stephan talking about it.
I've already talked with Stephan about it and he came up with example above. I just seen this tool https://github.com/lars-erik/Umbraco.CodeGen and it uses this to to the mapping:
v6/7 API equivalent for uQuery.GetUmbracoObjectType
Hi All,
I have been working on a value converter for the v7 multinode tree picker, this value converter doesn't know if the picker is set to pick "content" or "media" but it needs to know.
I have previously used (and can still use) a really useful uQuery method called GetUmbracoObjectType which makes this really simple but I'm wondering if there is a current API method that does the same or if not should there be one?
An alternative approach is to look at the prevalue setting itself, but this is a lot more complicated, here's how it looks:
Thanks in advance!
Jeavon
Yes, ApplicationContext.Current.Services.EntityService.GetObjectType(into id)
This will return an UmbracoObjectTypes enum, which I believe is what you are looking for.
- Morten
Hey Morten,
Looks totally perfect, however I'm getting an exception when passing in a node id?
Exception:
Thanks
Jeavon
Oops, that doesn't look good. What type of item are you fetching the UmbracoObjectType for?
- Morten
Ah, its doing an ExecuteScalar<string> on nodeObjectType which is actually a nullable Guid. So that's probably where the conversion issue is happening :-S
Should be easy to fix though.
- Morten
It's just a ContentItem, jut tried with a Media item and same exception
Jeavon
Ah ha, like easy enough fix to make it into v7.1 RTM?
I dit it like this (not so clean):
I've got a other problem that maybe someone here know. If building a object generator and i need to find out if an object is a string or an other object. I've got the DataType like this:
But i can't find what type of object that is. I've seen that the umbracoWidth is the Type int but where can i find that in the DataTypeDefinition?
The bug with GetObjectType has been fixed in the upcoming v7.1.5
DataTypeService don't have a GetObjectType.. or must i use the EntityService to get the datatype object type?
Yes, you need to use EntityService to get the Umbraco object type.
What is it you are creating?
I've building some kind of Entity Framework for Umbraco. I already have the mapping and save in the open source project
http://our.umbraco.org/projects/developer-tools/w3s-umbraco-object-relation-mapper
see how it works https://github.com/W3S-uORM/uORM
and it would be nice if you have a render class that create the objects automatically.
Ah ok, do you know about IPublishedContentModelFactory?
Watch Stephan's session from CodeGarden at around 45 minutes http://stream.umbraco.org/video/9921022/core-internals-for-website-development
whoo double..
Did not know that Jeavon! Thanks, But because i got my own ORM (with save and dependency injection) i need to write my own that works with that ORM. But i can look at his code how he finds the type. Thanks!
Jeavon, maybe you know this. I'm trying to find the ClrType from a object. This is what is got:
But if i look at the mediatype image, umbracowidth it says it's a object. But in a debugger it says it is a Int? Where can i find that ClrType that is that Int?
I'm afraid I don't but IPublishedContentModelFactory is not part of Stephan's model generator, it is a hook built into the Umbraco v7.1.4 Core for creating projects such as this. I believe it may expose the types of all the properties in the installation (taking into account value converters) as I think I remember Stephan talking about it.
I've already talked with Stephan about it and he came up with example above. I just seen this tool https://github.com/lars-erik/Umbraco.CodeGen and it uses this to to the mapping:
So i don't think it's possible?
is working on a reply...