I'm building a search feature which effectively
returns a result set of IDs, the node IDs could relate to both page and media nodes. I'm trying to work out the best way of working out which type of node I'm dealing with.
I have this (which does work) but it doesn't feel very clean?
int id = @result.Id; // verbose line for demonstration purposes
dynamic pageNode = new DynamicNode(id); dynamic mediaNode = @Model.MediaById(id); bool IsMediaNode = false; if (mediaNode.HasValue("umbracoFile")) { IsMediaNode = true; }
Thanks Rodion, I had been looking for that property so it's good that I now know it. However for this specific example it would further complicate the code. To determine whether or not a node is part of the media library I would have to check against every media node type alias.
(Minor correction, the namespace is umbraco.cms.businesslogic.media)
I did start looking at testing against media vs document GUIDs @{umbraco.cms.businesslogic.web.Document._objectType;} but couldn't figure out how to get the GUID for the node to compare against.
Your code seems more abstracted from Umbraco internals which should be better.
Everything that inherits CMSNode (Document, Media, etc) has two ID properties - the "Id" (integer node id that we usually deal with using API) and the "UniqueId" (guid id that's mostly used internally in the database).
There's an easier way of doing this. Just use the DynamicBackingItem class (in umbraco.MacroEngines namespace). This has a property called Type which tells you whether it is Media or Content. For example:
int mediaId = 1139;
DynamicBackingItem bt = new DynamicBackingItem(mediaId);
<div>@bt.Type </div> // Media
int pageId = 1052;
bt = new DynamicBackingItem(pageId);
<div>@bt.Type</div> // Content
Best way of determining if node is media node
I'm building a search feature which effectively returns a result set of IDs, the node IDs could relate to both page and media nodes. I'm trying to work out the best way of working out which type of node I'm dealing with.
I have this (which does work) but it doesn't feel very clean?
int id = @result.Id; // verbose line for demonstration purposes
dynamic pageNode = new DynamicNode(id);
dynamic mediaNode = @Model.MediaById(id);
bool IsMediaNode = false;
if (mediaNode.HasValue("umbracoFile")) {
IsMediaNode = true;
}
Thanks Rodion, I had been looking for that property so it's good that I now know it. However for this specific example it would further complicate the code. To determine whether or not a node is part of the media library I would have to check against every media node type alias.
For example;
bool IsMediaNode = false;
if (mediaNode.NodeTypeAlias.Equals("Image") || mediaNode.NodeTypeAlias.Equals("Document") || mediaNode.NodeTypeAlias.Equals("Video")) {
IsMediaNode = true;
}
Unless I'm missing something?
Just don't forget to add "@using umbraco.businesslogic.media" to the start of the file.
That's great thanks!
(Minor correction, the namespace is umbraco.cms.businesslogic.media)
I did start looking at testing against media vs document GUIDs @{umbraco.cms.businesslogic.web.Document._objectType;} but couldn't figure out how to get the GUID for the node to compare against.
Your code seems more abstracted from Umbraco internals which should be better.
Thanks again.
Everything that inherits CMSNode (Document, Media, etc) has two ID properties - the "Id" (integer node id that we usually deal with using API) and the "UniqueId" (guid id that's mostly used internally in the database).
There's an easier way of doing this. Just use the DynamicBackingItem class (in umbraco.MacroEngines namespace). This has a property called Type which tells you whether it is Media or Content. For example:
is working on a reply...