Copied to clipboard

Flag this post as spam?

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


  • Steve 472 posts 1216 karma points
    May 26, 2016 @ 15:49
    Steve
    0

    Get complete listing of all Media files with .doc and .pdf extensions

    I would like to pull a list of all the Media files with .doc and .pdf extensions using a Razor Macro in Umbraco 6.1.6, but I am not quite sure of how to accomplish this.

    Could someone make some suggestions? Thanks! Here is what I have so far, but it fails with errors.

    @{
    
         var MediaFolder = Library.NodeById(Parameter.mediaFolder);
    
         var media = Model.MediaById(MediaFolder);
        var files = media.Children;
    }   
    @{
         foreach(var file in files)
         {
             var fileUrl = file.umbracoFile; 
            if(file.umbracoExtension == "pdf")
            {
          <a href="@fileUrl">
             @file.Name @file.umbracoExtension
          </a>
             }
    
        if(file.umbracoExtension == "doc")
            {
             <a href="@fileUrl">
             @file.Name @file.umbracoExtension
          </a>
                }
             else
             {
                 <p>not a document</p>
             }
            }
    }
    
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    May 27, 2016 @ 13:59
    Dennis Aaen
    0

    Hi Steve,

    I think something like this will do what you are after.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{ var mediaId = Model.MacroParameters["mediaFolder"]; }
    @if (mediaId != null)
    {
        @* Get all the media item associated with the id passed in *@
        var media = Umbraco.Media(mediaId);
        var selection = media.Children();
    
        if (selection.Any())
        {
            <ul>
                @foreach (var item in selection)
                {
    
                        if(item.umbracoExtension == "pdf"){
                            <li>
                                <a href="@item.Url">
                                    @item.Name @item.umbracoExtension
                                </a>
                            </li>
                        }
    
                        if(item.umbracoExtension == "doc"|| item.umbracoExtension == "docx"){
                            <a href="@item.Url">
                                    @item.Name @item.umbracoExtension
                            </a>
                        }
                }
            </ul>
        }
    }
    

    Hope this helps,

    /Dennis

  • Steve 472 posts 1216 karma points
    May 27, 2016 @ 14:08
    Steve
    0

    Thanks for your suggestion Dennis, but I am using Webforms, not using MVC

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    May 27, 2016 @ 14:15
    Dennis Aaen
    0

    Hi Steve,

    I have tested this on a Umbraco 6.1.6 If you go to developer section and create a partial view marco file, then you can use the code snippet from my previous post.

    /Dennis

  • Steve 472 posts 1216 karma points
    May 27, 2016 @ 14:19
    Steve
    0

    When I try to create a partial view like you suggest, I get a system error within the backend.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    May 27, 2016 @ 15:06
    Dennis Aaen
    0

    Okay perhaps you can find more info on why you are getting this in the log file

    /App_Data/Logs

    /Dennis

  • Steve 472 posts 1216 karma points
    May 27, 2016 @ 15:15
    Steve
    0

    Using my original code I can get the top level media items, but nothing else. If I change my files variable to be media.Descendants() the script won't even load. What's up with that?

Please Sign in or register to post replies

Write your reply to:

Draft