Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Robert Dolman 22 posts 42 karma points
    Jan 25, 2012 @ 10:46
    Robert Dolman
    0

    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;
        }

  • Rodion Novoselov 694 posts 859 karma points
    Jan 25, 2012 @ 11:30
    Rodion Novoselov
    0
    var nodeTypeAlias = (new DynamicNode(id)).NodeTypeAlias;
  • Robert Dolman 22 posts 42 karma points
    Jan 25, 2012 @ 11:41
    Robert Dolman
    0

    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?

  • Rodion Novoselov 694 posts 859 karma points
    Jan 25, 2012 @ 12:08
    Rodion Novoselov
    0
    var alias = (new DynamicNode(id)).NodeTypeAlias;
    if(MediaType.GetAllAsList().Any(_mt => _mt.Alias == alias)) {
    @:Hello, MediaType!
    }

    Just don't forget to add "@using umbraco.businesslogic.media" to the start of the file.

  • Robert Dolman 22 posts 42 karma points
    Jan 25, 2012 @ 13:40
    Robert Dolman
    0

    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.

  • Rodion Novoselov 694 posts 859 karma points
    Jan 25, 2012 @ 13:47
    Rodion Novoselov
    0

    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).

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jan 25, 2012 @ 17:07
    Dan Diplo
    1

    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
Please Sign in or register to post replies

Write your reply to:

Draft