Sunday, January 10, 2016

Sitecore MVC with Dynamic Placeholder

Introduction
This is demo project with Car example.

Technical Use Cases
  • Dynamic Placeholder with configured pipeline.
  • View Rendering and Controller Rendering 
  • Static Placeholder
  • Repository to create Sitecore.Mvc.Presentation.RenderingContext Context.

Sitecore Design Layer

  • Layouts
  • Placeholders
  • Renderings
  • Items
  • Reference -Data collection

Sitecore MVC
  • View- Layouts
  • View- Container 
  • View-TextImage
  • View-Header
  • View-Footer
  • Controller- FooterController
  • Controller- TextImageController
  • Controller- HeaderController
  • Repository for above Controller


Three column Dynamic Placeholder

@Html.Sitecore().DynamicPlaceholder("sub-content-three-column-content-left")
@Html.Sitecore().DynamicPlaceholder("sub-content-three-column-content-middle")
@Html.Sitecore().DynamicPlaceholder("sub-content-three-column-content-right")
Extend Sitecore MVC using SitecoreHelper Sitecorehelper to extend the @Html.Sitecore.Placeholder("")
namespace Demo.Sitecore.CarInfo.WebSite.Utility
{
    public static class SitecoreExtensions
    {
        public static HtmlString DynamicPlaceholder(this SitecoreHelper helper, string key)
        {
            Guid currentRenderingId = RenderingContext.Current.Rendering.UniqueId;
            return helper.Placeholder(string.Format("{0}#{1}", key, currentRenderingId.ToString()));
        }
    }
}

Ultimately this dynamic placeholder fetch the textimage for respective layouts from 
TextImage Controller Rendering.



TextImageRender Repository

using Demo.Sitecore.CarInfo.WebSite.Models;
using Sitecore.Mvc.Presentation;
using Sitecore.Web.UI.WebControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Demo.Sitecore.CarInfo.WebSite.Repository
{
    public class TextImageRepository
    {
        public TextImage GetTextImage()
        {
            var item = RenderingContext.Current.Rendering.Item;
            var textImage = new TextImage()
            {
                Title = new HtmlString(FieldRenderer.Render(item, "Title")),
                Image = new HtmlString(FieldRenderer.Render(item, "Image"))
            };
            return textImage;
        }
    }
}

TextImage Model


namespace Demo.Sitecore.CarInfo.WebSite.Models
{
    public class TextImage
    {
        public HtmlString Title         { get; set; }
        public HtmlString Description   { get; set; }
        public HtmlString Image         { get; set; }
    }
}
TextImage Controller
namespace Demo.Sitecore.CarInfo.WebSite.Controllers
{
    public class TextImageController : SitecoreController
    {
        public ActionResult TextImageDetail()
        {
            TextImageRepository repo = new TextImageRepository();
            return View(repo.GetTextImage());
        }
    }
}

Create Pipeline to Process DynamicPlaceholderRendering

namespace Demo.Sitecore.CarInfo.WebSite.Utility
{
    public class GetDynamicPlaceholderRenderings : GetAllowedRenderings
    {
        public new void Process(GetPlaceholderRenderingsArgs args)
        {
            Item placeholderItem;
            var placeholderKey = string.Join("/", args.PlaceholderKey.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Split('#')[0]));

            using (new DeviceSwitcher(args.DeviceId, args.ContentDatabase))
                placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase, args.LayoutDefinition);

            List list = null;
            if (placeholderItem != null)
            {
                args.HasPlaceholderSettings = true;
                bool allowedControlsSpecified;
                list = GetRenderings(placeholderItem, out allowedControlsSpecified);

                if (allowedControlsSpecified)
                    args.Options.ShowTree = false;
            }

            if (list == null)
                return;

            if (args.PlaceholderRenderings == null)
                args.PlaceholderRenderings = new List();

            args.PlaceholderRenderings.AddRange(list);
        }
    }
}








Page Editor- Select Allow Controls 

TextImage Renderings



No comments :