The Better Solution: Injecting Resources into the Experience Editor

Friday, July 7th I began my new series “A Different Approach” with the post about injecting resources into the experience editor. After I made my that post live, I continued my research and I became increasingly unhappy with my solution described. There is a better solution that solves my issue and that solution is somewhere in this post! 😮  Continue reading “The Better Solution: Injecting Resources into the Experience Editor”

“All it takes to turn a grouchy Experience Editor Friendly is a little TLC.” – Mr. Rogers

♬ It’s a beautiful day in the Experience Editor, A beautiful experience for an author, since we’re together, we might as well say, won’t you be my Content Author? ♬

Hi, Sitecore Neighbor, I’m glad we’re together again… discussing new cool ways we can help provide the friendliest authoring experience an author can have while using Sitecore’s Experience Editor! Continue reading ““All it takes to turn a grouchy Experience Editor Friendly is a little TLC.” – Mr. Rogers”

Don’t Let a Datasource Error Delay Your Weekend of LARPing

It’s 4:59PM Friday afternoon and your long work week is almost over; one minute away from 48 hours of non-stop, fun-filled, Mountain Dew fueled LARPing.  Just as you stand up to leave, your work phone rings and it’s your favorite client.  Going against your better judgement you decide to be nice and answer.

“Help Hasagn Sailorslayer!  Thoust Dragon Smaug breathed fire on our site and tis no more!”… the client says while speaking with a horrible British accent. “I swear, all I did was a publish and now the entire site is erroring!”.

The client provides you with all the vague, helpless information you’ve come to expect and now your weekend of throwing lightning bolts may be delayed.

After a half hour and a couple hundred “WTFs” you remember Sitecore troubleshooting basics.  You check the logs and find an object reference error on the “Main Navigation”; somehow the client deleted the Main Navigation’s datasource item while ignoring common sense and Sitecore’s warnings.  You restore the deleted item and the crisis has been adverted, the client is happy and you begin to rethink the client’s request of needing full admin privileges.

There are a lot of ways we could prevent this error from happening. Continue reading “Don’t Let a Datasource Error Delay Your Weekend of LARPing”

Sitecore View Renderings – Implementing Views for the Experience Editor

One of my biggest areas of focus developing with Sitecore the last two years has been the Experience Editor. I am passionate about providing clients a clean, robust, confusion free experience while they edit their content.

Sometimes I see components that are not very Experience Editor friendly, like a ‘Mega Nav’. Due to expanding nature of the navigation, it is very difficult to edit inline. We could alter the CSS to stack the Nav drop downs when editing them, but that’s not always ideal. What if we have other components that need stacked to provide an easy editing experience? This could make the page extremely long.

The solution is to add “Experience Editor only” markup. Then when the user is editing the component, they can see an expanded view of the navigation that makes it easy to edit.

In Sitecore, I create one Mega Nav view rendering called MegaNavigation. In the file, I then create two views, the normal “MegaNavigation.cshtml” and the Experience Editor only view “MegaNavigation_EE.cshtml”.

In order for this to work we need to override the GetViewRenderer pipeline.

GetViewRenderer

Config Settings
Place this in your site’s config:

Enabling Experience Editor with View Rendering is pretty simple. But what about Controller Renderings? Well that’s a little more complex and I will cover that in another blog post. Hint, it involves creating a new Razor View Engine.


Do you enjoy my oddly themed blogs and wish you had access to even more of me and my ideas? Good news, you’re in luck!

If 140 characters is your thing, follow me on Twitter.


If you hate reading and watching Sitecore videos entertains you, head over to my YouTube channel! Sometimes I entertain, sometime I provide useful Sitecore information and sometimes I can do both in the same video.


I can also be found hanging out on the Sitecore channels on Slack, I like it, although it occasionally triggers AOL chat room flashbacks from the olden days.


You can also find me adding content on LinkedIn and on Reddit.

Handling Image Dimensions in the Richtext Editor

Something odd happens when an image is added in the rich text editor. Initially, when the image is inserted, the RTE places the width and height properties correctly. However, when we change the properties such as adding a class, the dimensions are now inline styled. Inline styling dimensions isn’t responsive friendly.

Clients should not be expected to edit the HTML. So how do we resolve this issue?

When the client is making changes in the RTE we can fire off code to alter the HTML when the RTE is closed. We can accomplish this by utilizing the saveRichTextContent pipeline.

For content items that haven’t been modified after the new pipeline has been added, we can create a new renderField pipeline.

First we create the code to cycle through the images in the HTML and modify the inline styles.

using System;
using System.Linq;
using HtmlAgilityPack;

namespace Company.Library.Pipelines.RichTextContent
{
    public static class RichText
    {
        public static string Content(string content)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(content);

            if (!doc.DocumentNode.Descendants("img").Any())
                return content;

            foreach (var img in doc.DocumentNode.Descendants("img"))
            {
                var imgAttr = img.Attributes["style"];

                if (imgAttr == null)
                    continue;

                var alteredStyles = string.Empty;
                var inlineStyleList = imgAttr.Value.Split(';');
                var removeImageDimensions = Settings.GetBoolSetting("RTE.Images.RemoveImageDimensions", false);

                foreach (var style in inlineStyleList)
                {
                    var attrList = style.Split(':');

                    if (!attrList.Count().Equals(2))
                        continue;

                    var property = attrList[0].Trim();
                    var val = attrList[1].Trim();

                    if ((property.Equals("width", StringComparison.InvariantCultureIgnoreCase) ||
                        property.Equals("height", StringComparison.InvariantCultureIgnoreCase)) && !removeImageDimensions)
                        img.Attributes.Add(property, val);
                    else
                        alteredStyles += string.Format("{0}:{1}; ", property, val);
                }

                imgAttr.Remove();

                if (!string.IsNullOrWhiteSpace(alteredStyles))
                    img.Attributes.Add("style", alteredStyles);
            }

            return doc.DocumentNode.OuterHtml;
        }
    }
}

saveRichTextContent Pipeline

using Sitecore.Shell.Controls.RichTextEditor.Pipelines.SaveRichTextContent;

namespace Company.Library.Pipelines.RichTextContent
{
    public class Save
    {
        public void Process(SaveRichTextContentArgs args)
        {
            args.Content = RichText.Content(args.Content);
        }
    }
}
Before:
<img style="width: 200px; height: 200px; border: solid 3px #000;" alt="" src="/someimage.png" />
After:
<img width="200" height="200" style="border: solid 3px #000;" alt="" src="/someimage.png" />

Config Settings

Create a custom config file in the App_Config/Include folder if you haven’t done so and add the following:

  
    	 	 
   	 	 
      	 	 
          	 	 
      	 	 
      	 	 
         	 	 
             	 	 
         	 	 
      	 	 
   	 	 


Video Demo


Do you enjoy my oddly themed blogs and wish you had access to even more of me and my ideas? Good news, you’re in luck!

If 140 characters is your thing, follow me on Twitter.


If you hate reading and watching Sitecore videos entertains you, head over to my YouTube channel! Sometimes I entertain, sometime I provide useful Sitecore information and sometimes I can do both in the same video.


I can also be found hanging out on the Sitecore channels on Slack, I like it, although it occasionally triggers AOL chat room flashbacks from the olden days.


You can also find me adding content on LinkedIn and on Reddit.