0

I am trying to create a carousel in Sitecore 8.2. I have created a carousel item with the Image and Text fields and these carousel items are mapped to Multilist.

I am getting a null reference error while rendering the multilist items in Visual Studio.

My View code:

<div id="home-carousel" class="jcarousel-wrapper">
<a href="#" class="jcarousel-control-prev">&lsaquo;</a>
<a href="#" class="jcarousel-control-next">&rsaquo;</a>

<div class="jcarousel carousel-stage">
<asp:Repeater ID="rptCarousel" ItemType="Sitecore.Data.Items.Item" runat="server">
<HeaderTemplate>
<ul class="carousel">
</HeaderTemplate>

<ItemTemplate>
<li>
<sc:Image Field="Carousel Image" Item="<%# Container.DataItem %>" runat="server" />
<sc:Text Field="Carousel Text" Item="<%# Container.DataItem %>" runat="server" />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</div>
</div>

My back-end code:

public partial class carousel : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var sublayout = Parent as Sublayout;
            var datasource = sublayout.DataSource;
            Item Carousel = Sitecore.Context.Database.GetItem(datasource);
            Item[] item = Carousel.Children.ToArray();
            rptCarousel.DataSource = item;
            rptCarousel.DataBind();
        }
    }

Sitecore Content

Carousel Template UPDATE CODE FROM COMMENT

Sitecore.Data.Fields.MultilistField multilistField = Sitecore.Context.Item.Fields["Select Item"]; 
if (multilistField != null && multilistField.TargetIDs.Any()) 
{
    foreach (ID id in multilistField.TargetIDs)
    {
        Item targetItem = Sitecore.Context.Database.Items[id];
        Item[] item = targetItem.Children.ToArray();
        rptCarousel.DataSource = item; 
        rptCarousel.DataBind(); 
    } 
}
  • From the code provided, it does not seems that you are reading the MultiList Field. Can you please share how it looks into Sitecore content tree? – Hishaam Namooya Sep 16 at 8:52
  • @HishaamNamooya I have added Sitecore Content tree references please check.. – Adya Sep 16 at 9:35
  • 1
    one if the carousel items, or one of the images in them, has not been published – Mark Cassidy Sep 16 at 9:39
  • as @HishaamNamooya said, you are doing your code incorrect way, where you are getting all the child items of the datasource item, from the above tree it looks like you have multilist field on your current item and it is not a datasource driven control. is this correct? – Mahendra Shekhawat Sep 16 at 10:12
  • @HishaamNamooya yes I am using Multilist . – Adya Sep 17 at 11:15
0

Based on comments above, the first issue is that you have a MultiList but the code is not reading from it. Upon the update of the code from the comment section, please see below the updated one.

Problem

The updated code you've provided is not correct as it is initializing the array everytime as it is in the loop and also, you are performing a GetChildren() on the selected item, which is the carousel item itself.

Solution

Sitecore.Data.Fields.MultilistField multilistField = Sitecore.Context.Item.Fields["Select Item"];

if (multilistField != null && multilistField.TargetIDs.Any())
{
    var selectedItems = multilistField.GetItems();

    rptCarousel.DataSource = items;

    rptCarousel.DataBind();
}

If GetItems() method does not exist in your current Sitecore.Kernel, which should not be the case, you will need to amend the above code to the one below

Sitecore.Data.Fields.MultilistField multilistField = Sitecore.Context.Item.Fields["Select Item"];

if (multilistField != null && multilistField.TargetIDs.Any())
{
    ArrayList carouselArray = new ArrayList();

    foreach (ID targetId in multilistField.TargetIDs)
    {
        Item carouselItem = Sitecore.Context.Database.GetItem(targetId);
        if (carouselItem != null)
        {
            carouselArray.Add(carouselItem);
        }
    }

    rptCarousel.DataSource = carouselArray;

    rptCarousel.DataBind();
}

Note that you can create an extension for the MultiList field and adds the code that perform the retrieving of the selected items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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