Copied to clipboard

Flag this post as spam?

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


  • Mark Evans 86 posts 116 karma points
    Sep 05, 2014 @ 16:47
    Mark Evans
    0

    accessing media folders by name

    Im trying to code for a scenario whereby different sections/divisions of our website will have an appropriate folder within media/images for their related images (with sub folders in these for sections under them and different types of image... news, sliders etc)

    this allows us to restrict the access to media in the same way at a level like content....

    now im attempting to show logos on each page of the site that has them loaded... so level 3 page C will access media folder "logos" within its corrsponding folder 'level3page' and page B at level 2 will access logos for folder "logos" at its level AND any "logos" folders below it (level 3) and Home at top level will access its own "logos" folder and all sub-folders of "logos" below it.

    i have a lot of if else statements trying to cater for all combinations of structure but wondered if there is a way to be at any level and do a find for all folders called "logos" BELOW that level rather than say keep checking for a folder under another folder etc etc....and checking if its called "logos" then checking if children is an image.....

  • John C Scott 440 posts 1076 karma points
    Sep 05, 2014 @ 17:44
    John C Scott
    0

    I like the elegance of what you're trying to do but I'm not really sure if what I'm suggesting here would work but it would be nice if it did. You could use XPath with UQuery possibly. Xpath support for media has never been great, and it's not getting better. The type of media object returned is the old namespace and not the current ones, but it could work. So you could have something like:

    @{
    IEnumerable<umbraco.cms.businesslogic.media.Media> uqMedia = uQuery.GetMediaByXPath("//folder [name='page3']::decendents/logos");
    foreach (var logo in uqMedia) {
    Media coreMedia = Umbraco.Media(logo.Id);
    <img src="@coreMedia.Url" alt="@child.Name">
    }

    It's a thought anyway.

    It's probably safer to have a different approach to this, but this is an idea that may or may not work, I vaguely recall that the xpath for media items is not as full as you might want it to be, as there is no equivalent to the appdata/umbraco.config XML file for media items. 

  • Mark Evans 86 posts 116 karma points
    Sep 05, 2014 @ 17:55
    Mark Evans
    0

    Thanks i will look into this next monday now....

    In simple terms im wanting to start at any level within the Media > Images folder tree... then be able to say get all images that are within all folders called "logos".... BELOW that level only....

    (currently there is Home > logos and all other levels go a maximum 2 levels... ie level 1 (same level as home) - logos folder  plus 1 or more level 2 folders - each with a logos folder) but its possible the company may add further sub levels in the future and be nice to cater for these dynamically rather than adding another block of code to check another level of folder/folder name/images...... (hope this makes sense)

     

  • John C Scott 440 posts 1076 karma points
    Sep 08, 2014 @ 10:25
    John C Scott
    0

    There is an interesting quote on the docs page:

    uQuery
    uQuery was originally created in the uComponents project primarily to overcome some of the missing features of the legacy NodeFactory and other query techniques that came out of the box with Umbraco. uQuery was eventually integrated into Umbraco's core. Just like DynamicNode, uQuery has been superceded by UmbracoHelper.
    *** NOTE ** If there are features of uQuery that are not available via UmbracoHelper **
    *** please create a task/issue on the tracker so we can implement it. ***

    uQuery is similar to DynamicNode in that is adds tree traversal/filtering methods and acts as a wrapper to the website cache. uQuery extends the NodeFactory, Document, Media, Member and Relations apis and can be queried using LINQ.
    The property accessor syntax is heavier, but is strongly typed, so there's intellisense.

    I've highlighted the point above with pretty stars. It might seem academic but it's a really good point. If we're planning to support (or is currently supported I'm not getting quite up to date with releases) an enhanced XPATH then it would be good if this supported similar to content for media. This is when open souce works best, when useful features developed as a user initiated improvement to the core like UQuery by Hendy - becomes incorporated into a better specified and implemented core to latest standards. It might be there is a simple solution with LINQ and a dynamic accessor to achieve this, especially with the xpath like syntax added to the dynamic core content object including accessors similar to :DESCENDENTS in the predicate above ^^^

    Some one like Benjamin or Christian would be good to ask about this, and I'll tweet them and see if they can add anything obvious that I'm missing here.

    I'm also going to take a look at the source for the uQuery syntax and see how it gets it's source for the XML that it runs its' Xpath against for the Media library, this is open too. 

    If there is an issue or a feature with this, then please blog it, vote for it, submit it, and if you can make it better then patch it :) Open source is beautiful. Happy Monday :D

    Umbraco is great when it works exactly as you describe above, as it means you can deliver a lot of value to your editor with very little cost investment of devops support time. In fact Umbraco works best like this. Enjoy and I hope your development journey & day is enjoyable. it's always better with the orange U.

    Looking forward to hearing how you get on.

     

  • John C Scott 440 posts 1076 karma points
    Sep 08, 2014 @ 10:59
  • David Armitage 190 posts 764 karma points
    15 days ago
    David Armitage
    0

    Hi Guys,

    Here is a good method I use to firstly to get the containing media folder by name. You can then look through the children.

    public static IPublishedContent GetFolderByName(string name, int? parentId = null)
            {
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    
                if(parentId != null)
                {
                    umbracoHelper.TypedMedia(parentId).Children("Folder").Where(x => x.Name == name).FirstOrDefault();
                }
    
                return umbracoHelper.TypedMediaAtRoot().DescendantsOrSelf("Folder").Where(x => x.Name == name).FirstOrDefault();
            }
    

    Then call the method here

    var mediaFolder = MediaManager.GetFolderByName("Some Folder Name");
                foreach (var mediaItem in mediaFolder.Children)
                {
                    //do whatever you want with the media here
                }
    
Please Sign in or register to post replies

Write your reply to:

Draft