Quantcast
Channel: Horizontal Integration » C#
Viewing all articles
Browse latest Browse all 30

Sitecore Override Render in UserControl for Items

$
0
0

I learned two great things from the Lauch Sitecore project by Chris Castle. You can get the project here: LauchSitecore.net.

1. Change the Icon for Sublayouts and Renderings so it looks sweet in your presentation details.

Sweet Icons

2. For component use controls. Override render and use the context item switcher. In Launch Sitecore, this was done in Website/Configuration/SiteUI/SitecoreUserControlBase.cs. SitecoreUserControlBase has overriden the Render method of System.Web.UI.UserControl. I used this in another project the other day, but in my example the UserControl is always bound to a particular item, in a particular folder.

My ascx file looks like this:

<div class="slab">
    <div class="wrapper">
        <div class="combo">
            <div class="combo_media">
                <sc:Link ID="_scLogoLink" runat="server" Field="Logo Link" CssClass="logo">
                    <sc:Image ID="_scLogo" runat="server" Field="Logo" />
                    <sc:Text ID="_scLogoAlternateText" runat="server" Field="Logo Alternate Text" />
                </sc:Link>
            </div>
            <div class="combo_bd">
                <div class="cta">
                    <div class="cta_message">
                        <span>
                            <sc:Text ID="_scMessage" runat="server" Field="Message" />
                        </span>
                    </div>
                    <div class="cta_action">
                        <sc:Link ID="_scFindLowesLink" runat="server" Field="Find Lowes Link" CssClass="btn btn-standard btn-standard-invert">
                            <sc:Text ID="_scFindLowesLinkText" runat="server" Field="Find Lowes Link Text" />
                        </sc:Link>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

The code I used on the back end is:

namespace Site.Website.Sublayouts.Controls
{
    public partial class MyUserControl : BaseUserControl // which extends System.Web.UI.WebControls
    {
        #region Methods

        protected override void Render(HtmlTextWriter writer)
        {
            Item folderItem = Sitecore.Context.Database.GetItem(new ID(Site.Website.Code.Constants.MyFolderId));
            if (folderItem != null)
            {
                Item settingsItem = folderItem.GetChildren().FirstOrDefault(e => e.TemplateID.ToString() == Site.Website.Code.Constants.SomeItemTemplateId);
                if (settingsItem != null)
                {
                    using (new ContextItemSwitcher(settingsItem))
                    {
                        base.Render(writer);
                    }
                }
            }
        }

        #endregion
    }
}

It’s nice because I no longer have to go through and set all the Sitecore control’s item properties. Not that it’s hard to do, but I just thought this was a nice way of doing things. Also, in the example above, if the item is not found the control doesn’t render which is what I want.

Special thanks to Chris Castle.



Viewing all articles
Browse latest Browse all 30

Trending Articles