Copied to clipboard

Flag this post as spam?

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


  • Kristján Harðar 2 posts 72 karma points
    Apr 12, 2018 @ 14:53
    Kristján Harðar
    0

    Umbraco.TypedContentAtXPath to find specific file in Media

    Hello !

    I am trying to use TypedContentAtXPath() to find a specific file in a specific subfolder in my Media library. The reason for this is that instead of using

    var mediaFolder = Umbraco.Media(folderId)
    var desiredfile = mediafolder.Children.Where(x => x.name == nameoffile)
    

    I would like to not create an IPublishedContent object for each item in the folder when I would use a linq query through it to find my file.

    I have been trying to do, for example:

    var folder = Umbraco.TypedContentAtXPath("//Folder");
    var folder = Umbraco.TypedContentAtXPath("//Folder[@name='pdf']");
    var folder = Umbraco.TypedContentAtXPath("//folder[@id='folderId']")
    //folderId comes from dictionary.
    

    And yet I can't seem to get any results. Or even reach the media folder at all.

    I haven't worked a lot with Xpath's or Xml's in the past, but I can't seem too find much documentation on this function and all the forum posts just end up going the default way, which I pointed in the beginning. Am I missing something like a decleration? Or even misunderstanding the ability of this function?

    To clarify, I need to find a specific media file, in a specific folder, that contains a specific string in its name, and is the newest one of the ones found in that folder. Using TypedContentAtXPath.

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Apr 12, 2018 @ 15:21
    Hendy Racher
    1

    Hi Kristján,

    The TypedContentAtXPath method will query the XML for the content tree, rather than the media tree, however there is a legacy API that where you can make an XPath query against the media data: umbraco.uQuery.GetMediaByXPath(), but it might be better to make a Lucene query instead.

  • Kristján Harðar 2 posts 72 karma points
    Apr 17, 2018 @ 15:18
    Kristján Harðar
    0

    Thank you, GetMediaByXpath() is exactly what I needed :)

Please Sign in or register to post replies

Write your reply to:

Draft