Friday, August 4, 2017

Set rendering datasource programmatically

using System;
using System.Linq;
using Sitecore.Data.Items;
using Sitecore.Layouts;

namespace SfN.layouts.Brainfacts2.sublayouts
{
    public partial class sltSlideshow : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Sitecore.Layouts.RenderingReference[] renderings = Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true);
            Item currentItem = Sitecore.Context.Item;
            LayoutDefinition layout = LayoutDefinition.Parse(currentItem[Sitecore.FieldIDs.LayoutField]);

            foreach (DeviceDefinition device in layout.Devices)
            {
                if (device.Renderings != null)
                {
                    for (var i = 0; i < device.Renderings.Count; i++)
                    {
                        RenderingDefinition rendering = (RenderingDefinition)device.Renderings[i];
                        if (rendering.ItemID == "{AC8F2E39-52EF-4863-BB0B-DADF640B1610}")
                        {
                            Item slideShowsContainer = Sitecore.Context.Database.GetItem("{55F4132E-AC93-4A9B-BE2D-E6F7D733DFCD}");
                            Item slideshow = slideShowsContainer.GetChildren().FirstOrDefault(x => x.Name == Sitecore.Context.Item.Name);

                            if (slideshow != null)
                            {

                                rendering.Datasource = slideshow.ID.ToString();  // creates duplicates rendering in the layout details window.  
                                                                                 //rendering.DynamicProperties.Where(p => p.Name == "s:ds").ToList().ForEach(p => p.Value = datasource);

                                var slides = slideshow.Children;

                                if (slides.Count > 0)
                                {

                                    rptSlides.DataSource = slides;
                                    rptSlides.DataBind();
                                }
                            }
                            else
                                pnlSlider.Visible = false;

                        }
                    }
                }
            }          
        }

      
    }

}



================

<%@ control language="C#" autoeventwireup="true" codebehind="sltSlideshow.ascx.cs" inherits="SfN.layouts.Brainfacts2.sublayouts.sltSlideshow" %>
<script src="/scripts/brainfacts2/slideshow.js" type="text/javascript"></script>
<asp:Panel runat="server" ID="pnlSlider">
    <div class="player-slideshow">
        <div id="slideshow" class="royalSlider contentSlider rsDefault rsAutoHeight rsHor rsFade rsWithThumbs rsWithThumbsHor" data-rs="{&quot;autoHeight&quot;:true,&quot;arrowsNav&quot;:true,&quot;arrowsNavAutoHide&quot;:false,&quot;arrowsNavHideOnTouch&quot;:false,&quot;fadeinLoadedSlide&quot;:true,&quot;controlNavigationSpacing&quot;:0,&quot;controlNavigation&quot;:&quot;thumbnails&quot;,&quot;imageScaleMode&quot;:&quot;none&quot;,&quot;imageAlignCenter&quot;:false,&quot;loop&quot;:false,&quot;loopRewind&quot;:true,&quot;numImagesToPreload&quot;:6,&quot;keyboardNavEnabled&quot;:true,&quot;usePreloader&quot;:true,&quot;sliderDrag&quot;:false,&quot;navigateByClick&quot;:false,&quot;fitInViewport&quot;:false,&quot;transitionType&quot;:&quot;fade&quot;}">
            <asp:Repeater ID="rptSlides" runat="server">
                <ItemTemplate>
                    <div>
                        <sc:image field="Image" runat="server" class="rsImg" item="<%# Container.DataItem as Sitecore.Data.Items.Item %>" />
                        <div class="caption">
                            <div class="content">
                            </div>
                            <div class="source">
                                <sc:text field="Caption" runat="server" item="<%# Container.DataItem as Sitecore.Data.Items.Item %>" />
                            </div>
                        </div>
                        <div class="slide-text">
                        </div>
                        <span class="rsTmb">
                            <sc:image field="Image" runat="server" class="rsImg" item="<%# Container.DataItem as Sitecore.Data.Items.Item %>" />
                        </span>
                    </div>
                </ItemTemplate>
            </asp:Repeater>
        </div>
    </div>
</asp:Panel>

No comments:

Post a Comment