Copied to clipboard

Flag this post as spam?

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


  • Andreas Persson 27 posts 78 karma points
    Mar 16, 2015 @ 22:14
    Andreas Persson
    1

    list files from an folder in umbraco

    Hi, does any one no if there is a package that can make a specific folder in the media section to get listed at the site for the end user?

    for exampel if i have a folder called "guides" i whould like the end user to be able to list all the pdf files in that folder 

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 16, 2015 @ 22:32
    Dennis Aaen
    0

    Hi Andreas,

    This should be a good starting point for you. With this Razor code you will list the files from a folder. First go to the developer section, create an new partial view macro file. Use this code in the file.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @*

        Macro to display a series of files from a folder.


        How it works:

            - Confirm the macro parameter has been passed in with a value

            - Loop through all the file Id's passed in (might be a single item, might be many)

            - Display any individual files, as well as any folders of images

        Macro Parameters To Create, for this macro to work:

        Alias:folderId     Name:Select folder with files    Type:Single Media Picker

    *@


    @{ var mediaId = Model.MacroParameters["folderId"]; }
        @if (folderId != null){

        @* Get all the media item associated with the id passed in *@
        var media = Umbraco.Media(folderId);
        var selection = media.Children("File");

        if (selection.Any()){
            <ul>
                @foreach (var item in selection){
                    <li>
                        <a href="@item.umbracoFile"">
                            @item.Name
                        </a>
                    </li>

                }
            </ul>

        }

    }

    You need to add the paramter in the developer section macro. Go to the developer section open up the macro folder, and choose the macro that contains your file that you just created. Then add the param  Alias:folderId Name:Select folder with files Type:Single Media Picker

    When you insert the file to your template, then you will have to choose the folder where you have the pdf files.

     

    Hope this helps, if you have further questions don't hesitate to ask again.

    /Dennis

  • Andreas Persson 27 posts 78 karma points
    Mar 16, 2015 @ 22:46
    Andreas Persson
    0

    Nice, thx alot, but how do i get the macro to be available in the richtexteditor?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 16, 2015 @ 22:55
    Dennis Aaen
    0

    Hi Andreas,

    You can enable the macro to be available in the richtext editor, by going to the developer section in the macro folder. Click on the macro that you just have created. In there you can set some different settings, one of them is Use in rich text editor, and if you are using Umbraco 7.2 it´s Use in rich text editor and the grid.

    In there you can also set if the macro should be cached.

    Hope this helps,

    /Dennis

  • Andreas Persson 27 posts 78 karma points
    Mar 16, 2015 @ 23:04
    Andreas Persson
    0

    Thx, but i got an error, 

    Error loading Partial View script

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 16, 2015 @ 23:20
    Dennis Aaen
    0

    Hi Andreas,

    Did you press the add button and then the save button, after you added the parameter to your macro.

    Hope this helps,

    /Dennis

  • Andreas Persson 27 posts 78 karma points
    Mar 16, 2015 @ 23:37
    Andreas Persson
    0

    yes this the code,

    How it works:
    
        - Confirm the macro parameter has been passed in with a value
    
        - Loop through all the file Id's passed in (might be a single item, might be many)
    
        - Display any individual files, as well as any folders of images
    
    Macro Parameters To Create, for this macro to work:
    
    Alias:folderId     Name:Select folder with files    Type:Single Media Picker
    

    *@

    @{ var mediaId = Model.MacroParameters["folderId"]; } @if (folderId != null){

    @* Get all the media item associated with the id passed in *@
    var media = Umbraco.Media(folderId);
    var selection = media.Children("File");
    
    if (selection.Any()){
        <ul>
            @foreach (var item in selection){
                <li>
                    <a href="@item.umbracoFile"">
                        @item.Name
                    </a>
                </li>
    
            }
        </ul>
    
    }
    

    }

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 16, 2015 @ 23:40
    Dennis Aaen
    0

    Hi Andreas,

    I have just updated the code there was some small issues to the first one that I posted. Try to see if this also works for you.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @*

        Macro to display a series of files from a folder.


        How it works:

            - Confirm the macro parameter has been passed in with a value

            - Loop through all the file Id's passed in (might be a single item, might be many)

            - Display any individual files, as well as any folders of images

        Macro Parameters To Create, for this macro to work:

        Alias:folderId     Name:Select folder with files    Type:Single Media Picker

    *@


    @{    
        var folderId = Model.MacroParameters["folderId"]; }
        @if (folderId != null){

        @* Get all the media item associated with the id passed in *@
        var media = Umbraco.Media(folderId);
        var selection = media.Children;

        if (selection.Any()){
            <ul>
                @foreach (var item in selection){
                    <li>
                        <a href="@item.umbracoFile">
                            @item.Name
                        </a>
                    </li>

                }
            </ul>

        }

    }

    Hope this helps,

    /Dennis

  • Andreas Persson 27 posts 78 karma points
    Mar 16, 2015 @ 23:42
    Andreas Persson
    0

    nopp, still error though

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 17, 2015 @ 00:00
    Dennis Aaen
    0

    Hi Andreas,

    Okay what you could try to do to find out what is give you the error is, try to add the code step by step, to see where it breaks.

    So first if you have this code in your file does it still error though

    @inheritsUmbraco.Web.Macros.PartialViewMacroPage

    @*

       
    Macro to display a series of files from a folder.


       
    How it works:

           
    -Confirm the macro parameter has been passed inwith a value

           
    -Loop through all the file Id's passed in (might be a single item, might be many)

            - Display any individual files, as well as any folders of images

        Macro Parameters To Create, for this macro to work:

        Alias:folderId     Name:Select folder with files    Type:Single Media Picker

    *@


    @{    
        var folderId = Model.MacroParameters["folderId"]; }
        @if (folderId != null){

        @* Get all the media item associated with the id passed in *@
        var media = Umbraco.Media(folderId);
        var selection = media.Children;

        if (selection.Any()){

        }

    }

    And on this way take it step by step to see where it´s breaks.

    Hope this helps,

    /Dennis

  • Andreas Persson 27 posts 78 karma points
    Mar 17, 2015 @ 00:03
    Andreas Persson
    0

    still get the error :-(

  • Andreas Persson 27 posts 78 karma points
    Mar 17, 2015 @ 00:19
    Andreas Persson
    0

  • Andreas Persson 27 posts 78 karma points
    Mar 17, 2015 @ 00:22
    Andreas Persson
    0

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 17, 2015 @ 00:52
    Dennis Aaen
    0

    Hi Andreas,

    Okay that a bit wired. But if you go the the developer section partial view folder, and try to create a new file, then in the dropdown for code snippets then choose List Images from Media Folder

    I have just tried this code at it works for me on Umbraco 7.2.2 And I only get the file type "File" out instead of images. I have also tried in the Grid layout and it works fine there too with this code.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage


    @*
        Macro to display a series of images from a media folder.

        How it works:
            - Confirm the macro parameter has been passed in with a value
            - Loop through all the media Id's passed in (might be a single item, might be many)
            - Display any individual images, as well as any folders of images

        Macro Parameters To Create, for this macro to work:
        Alias:mediaId     Name:Select folder with images    Type:Single Media Picker
    *@

    @{ var folderId = Model.MacroParameters["folderId"]; }
    @if (folderId != null)
    {
        @* Get all the media item associated with the id passed in *@
        var media = Umbraco.Media(folderId);
        var selection = media.Children("File");

        if (selection.Any())
        {
            <ul>
                @foreach (var item in selection)
                {
                    <li>
                        <a href="@item.umbracoFile">
                            @item.Name
                        </a>
                    </li>
                }
            </ul>
        }
    }

    What exact version of Umbraco are you using? Then I will see if I can reproduce what you are experience tomorrow on the same version like you are using.

    Hope this helps,

    /Dennis

  • hassan123-dev 6 posts 26 karma points
    Apr 09, 2024 @ 07:37
    hassan123-dev
    0

    Here is the correct way for this in version 13.

      @inherits Umbraco.Cms.Web.Common.Macros.PartialViewMacroPage
      @using Umbraco.Cms.Core
      @using Umbraco.Cms.Core.Routing
      @using Umbraco.Extensions
      @inject IPublishedContentQuery PublishedContentQuery
      @inject IPublishedUrlProvider PublishedUrlProvider
    
    @*
        Macro to display a series of files from a folder.
        How it works:
            - Confirm the macro parameter has been passed in with a value
            - Loop through all the file Id's passed in (might be a single item, might be many)
            - Display any individual files, as well as any folders of images
        Macro Parameters To Create, for this macro to work:
        Alias:folderId     Name:Select folder with files    Type:Single Media Picker
    *@
    @{ var folderId = Model.MacroParameters["folderId"]; }
    @if (folderId != null)
    {
        @* Get all the media item associated with the id passed in *@
        var media = Umbraco.Media(folderId);
        var selection = media.Children;
        if (selection.Any())
        {
            <ul>
                @foreach (var item in selection)
                {               
                    <li>
                        <a href="@item.Url(PublishedUrlProvider)" target="_blank" title="@item.Name">@item.Name</a>
                    </li>
                }
            </ul>
        }
    }
    
  • Andreas Persson 27 posts 78 karma points
    Mar 17, 2015 @ 01:06
    Andreas Persson
    0

    I did everything from the begining again and still got the error.

    iam running Umbraco version 7.1.8 assembly: 1.0.5394.16131

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 17, 2015 @ 05:29
    Jan Skovgaard
    0

    Hi Andreas

    Could you try setting up the code based on the standard macro "List Images From Media Folder" and then try commenting out the statements to see what line is causing the error by uncommenting it line by line? The {)'s pairs should of course not be uncommented if you need to test a statement inside it.

    Also I would like to know if the /App_Data/Logs file reveals any details about the error message? It should contain information about the macro you're using and mention what is the cause of the error.

    In regards to your folder where you're storing the PDF...is that a standard Folder or have you defined your own folder document type, which you use in the media archive?

    Looking forward to hearing from you.

    /Jan

  • Andreas Persson 27 posts 78 karma points
    Mar 17, 2015 @ 10:44
    Andreas Persson
    0

    hi, i have done that but still got the error, but i will try row by row now.

    i got this in the log

    Error loading Partial View (file: ~/Views/MacroPartials/ListPdf.cshtml). Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at ASP._Page_Views_MacroPartials_ListPdf_cshtml.Execute() in d:\home\U\www\test.com\Views\MacroPartials\ListPdf.cshtml:line 9 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) at Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult) at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, IPublishedContent content) at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, INode node) at umbraco.macro.LoadPartialViewMacro(MacroModel macro) at umbraco.macro.renderMacro(Hashtable pageEle

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 17, 2015 @ 11:19
    Jan Skovgaard
    0

    Hi Andreas

    Ok...perhaps you have some obscured items in the media section? But let's see how the line by line approach works out.

    /Jan

  • Andreas Persson 27 posts 78 karma points
    Mar 17, 2015 @ 15:10
    Andreas Persson
    0

    hi, i got it to generate correct now, but when i press publish the macro disaperas, got any clue why?

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 17, 2015 @ 15:36
    Jan Skovgaard
    0

    Hi Andreas

    Do you mind sharing the working code? :)

    Hmm, not sure why it happens - I remember seeing other people experience this issue as well. I suspect it's a bug in that particular version. Might be worth checking the issue tracker at http://issues.umbraco.org/issues

    /Jan

  • Andreas Persson 27 posts 78 karma points
    Mar 17, 2015 @ 15:49
    Andreas Persson
    1

    of course, here it is

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

     

     

    @*

        Macro Parameters To Create, for this macro to work:

        Alias:mediaId     Name:Media Folder ID    Type:Single Media Picker

    *@

     

    @if (Model.MacroParameters["mediaId"] != null)

    {

         @* Get the media folder as a dynamic node *@

        var mediaFolder = Umbraco.Media(Model.MacroParameters["mediaId"]);

        

        if (mediaFolder.Children.Any())

        {

            <ul>

                @* for each item in children of the selected media folder *@

                @foreach (var mediaItem in mediaFolder.Children)

                {

                    <li><a href="@mediaItem.umbracoFile">@mediaItem.Name<a/></li>

                }

            </ul>

        }

    }

     

    My problem was that it was called mediaId and not folderId, and the it worked i was also needed to change the

    <li><a href="@mediaItem.umbracoFile">@mediaItem.Name<a/></li> 

  • Andreas Persson 27 posts 78 karma points
    Mar 17, 2015 @ 17:28
    Andreas Persson
    0

    I couldn´t find any issue report on this though.

  • Andreas Persson 27 posts 78 karma points
    Mar 17, 2015 @ 17:38
    Andreas Persson
    1

    But the simple solution was that there was a "blank" in the name of the macro

Please Sign in or register to post replies

Write your reply to:

Draft