Wednesday, December 10, 2014

Media Items in Repeater

It's been a while since I've posted, but as I've been doing a lot of Sitecore work lately, here's a quick snippet of code I used to repeat a bunch of images directly from a media library folder.

<asp:Repeater runat="server" ID="rptPartners">
        <ItemTemplate>
            <li>
                <img src='<%# Sitecore.StringUtil.EnsurePrefix('/', Sitecore.Resources.Media.MediaManager.GetMediaUrl((Sitecore.Data.Items.Item)Container.DataItem)) %>' />
            </li>        
        </ItemTemplate>
    </asp:Repeater>

This little snippet is useful when a collection of images come directly from a folder within the media library. Here's the backend code:

var imagesFolder = Sitecore.Context.Database.GetItem(Sitecore.Context.Item.Fields["FIELD NAME"].Value);
rptPartners.DataSource = imagesFolder.Children;
rptPartners.DataBind();

I realize to some Sitecore purists out there, it's somewhat heretical to be mixing repeaters with Sitecore code, but coming from an ASP.NET developer's standpoint, I've found that a lot of ASP.NET / Sitecore hybrid solutions tend to be the quickest ways to get what I need DONE.  Hopefully this will be useful for someone out there!

No comments:

Post a Comment