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.
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.
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>
}
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.
@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>
}
}
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?
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
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
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
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.
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
Nice, thx alot, but how do i get the macro to be available in the richtexteditor?
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
Thx, but i got an error,
Error loading Partial View script
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
yes this the code,
*@
@{ var mediaId = Model.MacroParameters["folderId"]; } @if (folderId != null){
}
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.
Hope this helps,
/Dennis
nopp, still error though
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
And on this way take it step by step to see where it´s breaks.
Hope this helps,
/Dennis
still get the error :-(
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.
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
Here is the correct way for this in version 13.
I did everything from the begining again and still got the error.
iam running Umbraco version 7.1.8 assembly: 1.0.5394.16131
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
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
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
hi, i got it to generate correct now, but when i press publish the macro disaperas, got any clue why?
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
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>
I couldn´t find any issue report on this though.
But the simple solution was that there was a "blank" in the name of the macro
is working on a reply...