I am trying to create a partial view macro that displays the content of a media folder. The macro has a parameter called 'folder' and is a Media Picker. My intent is to select a specific media folder and then iterate through the content of the folder, looking for a specific custom media type, called 'Person'. The custom media type has the attributes 'Image', 'Name', and 'Address'. Right now I am just trying to get the folder. All I got so far for the partial view is below.
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
if (Model.MacroParameters["folder"] != null)
{
var myParam = Umbraco.Content(Model.MacroParameters["folder"]);
<div>@myParam.url.toString()</div>
}
}
This produces nothing. How do I know I have found the folder? How do I iterate through the folder's content? How do I check the content's media type?
You should not use the Content Picker for this purpose. You have to use the Media Picker to pick media folders.
Then I would suggest not using Umbraco.Content, because this is using dynamics. Use Umbraco.TypedMedia(folderId) instead. Also, Umbraco.Content() or Umbraco.TypedContent() picks not media, but content, like the name of the methods suggests.
You can use the following code to get all child media object by providing the media folder ID:
var folder = Umbraco.TypedMedia(Model.MacroParameters["folder"]);
if (folder != null && folder.DocumentTypeAlias == "folder")
{
foreach (var media in folder.Children().Where(x => x.DocumentTypeAlias == "Person"))
{
<img src="@media.Url" />
}
}
What you suggested works. Thank you very much! I did have to change the first doc type to "Folder".
Now the trouble I am running into is that the @media.Url value is empty. I don't know if it matters but this is a custom media type with an 'image', 'name', 'address' properties. The 'image' is the "Image Cropper" editor, same as the default "Image" media type.
I know that the folder has one "Person" image in it, as the foreach loop does put out the code. But the url is just empty.
Get Folder Content From Media Picker Parameter
Umbraco 7.13.2
I am trying to create a partial view macro that displays the content of a media folder. The macro has a parameter called 'folder' and is a Media Picker. My intent is to select a specific media folder and then iterate through the content of the folder, looking for a specific custom media type, called 'Person'. The custom media type has the attributes 'Image', 'Name', and 'Address'. Right now I am just trying to get the folder. All I got so far for the partial view is below.
This produces nothing. How do I know I have found the folder? How do I iterate through the folder's content? How do I check the content's media type?
You should not use the Content Picker for this purpose. You have to use the Media Picker to pick media folders.
Then I would suggest not using
Umbraco.Content
, because this is using dynamics. UseUmbraco.TypedMedia(folderId)
instead. Also,Umbraco.Content()
orUmbraco.TypedContent()
picks not media, but content, like the name of the methods suggests.You can use the following code to get all child media object by providing the media folder ID:
Thank you for the reply. I am sorry, I did mean to say 'Media Picker'. I am using the media picker. I will update the thread to be correct.
I will give your solution a try.
What you suggested works. Thank you very much! I did have to change the first doc type to "Folder".
Now the trouble I am running into is that the @media.Url value is empty. I don't know if it matters but this is a custom media type with an 'image', 'name', 'address' properties. The 'image' is the "Image Cropper" editor, same as the default "Image" media type.
I know that the folder has one "Person" image in it, as the foreach loop does put out the code. But the url is just empty.
Any thoughts?
I was able to figure it out. Basically, what you suggested was correct. I only had to change the following to get it to work:
Thank you very much for pointing me in the right direction. I will mark your response as the answer.
I think maybe its because you dont use the default
umbracoFile
alias. You named your propertyimage
.Interesting. I didn't realize the default alias came with it default properties. Maybe I will fiddle with that.
is working on a reply...