Sunday, March 17, 2019

Custom Sitecore Helpers


Why?

      Sometimes you need more functionalities and you wish you could extend the Sitecore HTML helper. I will show you two possibilities for how you can easily accomplish this.

Uses:

      It's very simple and your co-developers don't even need to know if this method is a Sitecore extension or your custom code, they can simply use it the same way as it would be a Sitecore helper method.

Extension methods:

The Asp.net HtmlHelper, as well as the SitecoreHelper class, are normal classes, that means you can write extension methods for it.So
If you want to extends the Sitecore HTML helper with a custom implementation, you just need to write an extension method for the SitecoreHelper class:

public static class SitecoreFieldHelper

    {
        public  static HtmlString CustomImageField(this SitecoreHelper helper, string fieldname,
             int mh, int mw, bool disablewebEditing = false)
        {

            return helper.Field(fieldname, new { mh, mw, DisablewebEditing = disablewebEditing });

        }
    }

Which you can call with:
   
 @Html.Sitecore().CustomImageField("Profile Theme", 200, 300, true)

Custom HtmlHelper

Extension methods have one big disadvantage: You can't override methods nor can you use protected methods of the "original" HTML helper. So another possibility is to write a custom HTML helper the same way as Sitecore does. The goal is to have something we can call like this:

  @Html.CustomMethod().MethodToCall()

Step 1:

       Here the concept is very easy. CustomMethod() is an extension method of the ASP.NET HtmlHelper and returns a new instance of our custom HTML helper, which inherits from the SitecoreHelper. First, we need the custom HTML helper with a new method:
    
  public class CustomHtmlHelper : SitecoreHelper
    {
        public CustomHtmlHelper(System.Web.Mvc.HtmlHelper htmlHelper)
        : base(htmlHelper)
        {
        }
        public virtual HtmlString CreateLabel(string key, string DefaultValue)
        {
            return new HtmlString(string.Format("<label'>{0}</label>", key));
        }

}

Step 2:

Last but not least we need to write the HtmlHelper extension method to get your custom helper:

   public static class HtmlHelperExtensions
    {
        public static CustomHtmlHelper CustomMethod(this HtmlHelper htmlhelper)
        {
            // get the helper from current thread
            var threadData = ThreadHelper.GetThreadData<CustomHtmlHelper>();
            if (threadData != null) return threadData;

            // create new helper if needed
            var helper = new CustomHtmlHelper(htmlhelper);
            ThreadHelper.SetThreadData(helper);
            return helper;
        }
}

Step 3:

       Now we are able to use it:

     @Html.CustomMethod().CreateLabel("Lable Static Text Here", "Label Name")

In theory, you are now able to "forget" the Sitecore HTML helper and always use your custom one. This would have a similar effect to your co-workers like extension methods. Because we inherit from the Sitecore HTML helper, you can also call its method over the custom class:

   @Html.CustomMethod().Field("My Field")

Happy coding !!!

Quotes Of the Day !!!

  Programs must be written for people to read, and only incidentally for machines to execute.


4 comments:

Steps to follow when using a PowerShell script to modify the goals in Sitecore

I have previously utilized PowerShell for item creation, modification, deletion, and presentation details in Sitecore.   Ø Recently, I attem...