I am using dxa's java project and need to implement a condition where I randomly pick an image out of a list of images.

I don't necessarily want to link 20 images to a component to fetch this list of media files.

I have kept the images (existing Image schema) in a folder under Content\Images\MyImages.

How can I publish this to Binary folder or DB and how do I get the list of these images so I can run my code to randomly select an image from it?

1 Answer 1


The easiest part is to get the images published: I would create a template building block that loops the folder and adds the images to the publishing package, with a specific file path.

Component mmComp = engine.GetObject(uri) as Component;
Tridion.ContentManager.Publishing.Rendering.Binary binary = engine.PublishingContext.RenderedItem.AddBinary(mmComp);
string publishedPath = binary.Url;
Item packageItem = package.CreateMultimediaItem(mmComp.Id);
package.PushItem(filename, packageItem);

Add this TBB to a CT or PT that you very seldom publish. Reason for this is that you just want to publish these images if you add new ones...

Not so easy/not tried is how to retrive them: I would try to use the BinaryMetaFactory to search for the specific file path. I'm more used to .Net than Java. Therefore the example below is with C#.

var binaryMetaFactory = new BinaryMetaFactory()
var binaryMetas = binaryMetaFactory.GetMetaByUrl(binaryUrl);

Hope this at least gives you an idea how to accomplish it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.