Sitecore MVC Autofac Dependancy Resolution

Recently we decided to integrate Autofac in our Sitecore MVC Solution. After a bit of research I found some pretty nice and useful articles on the topic of using Dependency Injection with Sitecore MVC, but there wasn’t anything focused on Autofac, so I decided to put a blog post around it. The approach is based on a blog article about Ninject, by Mark Cassidy Thomas Stern – Sitecore MVC new Ninject Controller Factory and the Sitecore Mvc Contrib amazing sample implementation by Kevin Obee.

So to build our Autofac Injection we basically need 4 things:

  1. An Autofac Container Factory, which is going to build the Autofac Container
  2. An Autofac Controller Factory, which is going to replace the Sitecore Controller Factory
  3. Replacement Pipeline that is going to initialize the Autofac Container and replace the default Sitecore controller Factory with the custom Autofac Controller Factory
  4. Configuring the pipeline to replace the default Sitecore Pipeline.

Building an Autofac Container Factory

The first step is to create an Autofac Container Factory. It needs to build the container, register the controllers in our scope and to leave us a place to register all other Modules,Types, etc. we need to register. For ease of use I am also going to use the Autofac MVC 5 Integration Package which provides a method called RegisterControllers which is going to be used in the Controller Registration. The sample code can be found bellow:


using System;
using Autofac;
using Autofac.Integration.Mvc;

namespace AutofacDependancyResolver.DependancyResolution.AutoFac.Factory
{
    public class AutofacContainerFactory
    {
        public IContainer Create()
        {
            var builder = new ContainerBuilder();

            // Register All Controllers In The Current Scope
            builder.RegisterControllers(AppDomain.CurrentDomain.GetAssemblies()).InstancePerRequest();

            // Register Modules
            builder.RegisterModule<ServicesModule>();

            // Register Additional Things
            // ...

            // Build The Container and return it as a result
            return builder.Build();
        }
    }
}

So what the code actually does is creating a new ContainerBuilder which is used to build the Container, registers all controllers in the current domain (only controllers), registration of a custom module (not required) and returns the built container as a result. And that is it – simple as that🙂

Building an Autofac Controller Factory

In order to build a custom Controller Factory, the class needs to implement from the DefaultControllerFactory. It comes with 2 Important Methods that must be overridden:

  1. GetControllerInstance – which is going to resolve the controller out of our IoC Container
  2. ReleaseController – which is irrelevant for Autofac Managed Application, as the release is done by the IoC container.

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;

namespace AutofacDependancyResolver.DependancyResolution.AutoFac.Factory
{
    public class AutofacControllerFactory : DefaultControllerFactory
    {
        private readonly IContainer _container;

        public AutofacControllerFactory(IContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            _container = container;
        }

        protected override IController GetControllerInstance(RequestContext context, Type controllerType)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (controllerType == null)
            {
                throw new HttpException(404,
                    string.Format("No Controller Type Found For Path {0}", context.HttpContext.Request.Path));
            }

            object controller;

            if (_container.TryResolve(controllerType, out controller))
            {
                return (IController) controller;
            }

            throw new HttpException(404,
                string.Format("No Controller Type: {0} Found For Path {1}", controllerType.FullName,
                    context.HttpContext.Request.Path));
        }

        public override void ReleaseController(IController controller)
        {
        }
    }
}

The code basically tries to resolve the controller from our IoC container. If no controller is found – a 404 exception is thrown.

The Pipeline !

So the last step is to register a pipeline that is going to replace the default controller factory with the Autofac Controller Factory. The code for the pipeline looks like this:


using System.Web.Mvc;
using Autofac.Integration.Mvc;
using AutofacDependancyResolver.DependancyResolution.AutoFac.Factory;
using Sitecore.Pipelines;

namespace AutofacDependancyResolver.DependancyResolution.AutoFac.Pipelines
{
    public class InitializeAutofacControllerFactory
    {
        public virtual void Process(PipelineArgs args)
        {
            SetControllerFactory(args);
        }

        private void SetControllerFactory(PipelineArgs args)
        {
            var containerFactory = new AutofacContainerFactory();
            var container = containerFactory.Create();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            var controllerFactory = new AutofacControllerFactory(container);
            ControllerBuilder.Current.SetControllerFactory(controllerFactory);
        }
    }
}

So the default controller factory is set to our controller factory ! Now the only thing remaining is to replace the pipeline with our own ! The configuration should be similar to:



<?xml version="1.0"?>

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <initialize>
        <processor type="AutofacDependancyResolver.DependancyResolution.AutoFac.Pipelines.InitializeAutofacControllerFactory, AutofacDependancyResolver.DependancyResolution" patch:before="*[@type='Sitecore.Mvc.Pipelines.Loader.InitializeControllerFactory, Sitecore.Mvc']" />
        <processor type="Sitecore.Mvc.Pipelines.Loader.InitializeControllerFactory, Sitecore.Mvc">
          <patch:delete />
        </processor>
      </initialize>
    </pipelines>
  </sitecore>
</configuration>


And that is it ! The Autofac resolving is ready to go !🙂

TL;DR;

You can find the example solution on Bitbucket !

Happy Injecting !

Sitecore SVG Support

SVG support is a topic that has already been discussed multiple times in blog posts, stackoverflow answers etc.

The main and the most common solution is to register the SVG MIME Type under the /sitecore/mediaLibrary/mediaTypes node like the following line.




<mediaLibrary>
      <mediaTypes>
        <mediaType name="SVG" extensions="svg">
          <mimeType>image/svg+xml</mimeType>
          <forceDownload>false</forceDownload>
          <sharedTemplate>system/media/unversioned/image</sharedTemplate>
          <versionedTemplate>system/media/versioned/image</versionedTemplate>
          <mediaValidator type="Sitecore.Resources.Media.ImageValidator"/>
          <thumbnails>
            <generator type="Sitecore.Resources.Media.ImageThumbnailGenerator, Sitecore.Kernel">
              <extension>png</extension>
            </generator>
            <width>150</width>
            <height>150</height>
            <backgroundColor>#FFFFFF</backgroundColor>
          </thumbnails>
        </mediaType>
      </mediaTypes>
    </mediaLibrary>


And that is it ! Now your SVG images can be safely uploaded to the Media Library !

However now you will encounter some problems when the content editors start uploading and using SVG Images, so I decided to put a blog post about them.

Note: All source code shown here is using Sitecore 8.1 Initial Release. Also because the code for the corresponding pages is pretty big I will just post the relevant fragments here. The full code of the solution can be found on BitBucket.

Now we’ve got the SVG images into the media library. The next step is to use them around the site right? Which leads me to:

Problem 1: My Rich Text Editor is not accepting SVGs !

Imagine one of our content editors decided to place the SVG image in a Rich Text Field somewhere in the site, sounds simple right ? Well, it does sound simple, but as it seems – it is not, because after trying to add the image via the Insert Sitecore Media button the following message appears:

Media Library

As it seems Sitecore still doesn’t like SVGs and it doesn’t treat them as images that can be placed in the Rich Text fields. So after spending some time with the decompiled Sitecore.Client assembly, the exception can be traced to the following line located in the InsertImageForm class in the OnOK function:


else if (!(MediaManager.GetMedia(MediaUri.Parse((Item) mediaItem)) is ImageMedia))
{
    SheerResponse.Alert("The selected item is not an image. Select an image to continue.");
}

So Sitecore is not considering our SVG as ImageMedia.

Note: Here is the time to say that I haven`t found an approach to register the SVG as ImageMedia. If someone has an idea I will be very grateful if he can post it in the comments section.

Well the fix is pretty straightforward – nothing fancy here. We need to hijack the dialog and add some custom logic to recognize our SVG as a real image.

The first thing we need to do is to create our custom class that will be an exact replica of the decompiled code of the InsertImageForm class, except the modified else/if statement which should be modified to as follows:

else if (!(MediaManager.GetMedia(MediaUri.Parse(mediaItem)) is ImageMedia || mediaItem.MimeType == "image/svg+xml"]))
{
    SheerResponse.Alert("The selected item is not an image. Select an image to continue.");
}

The next step is to make the dialog use our own class. The XML Layout file for this dialog is sitecore\shell\Controls\Rich Text Editor\InsertImage\InsertImage.xml. There we need to replace the following line:


<CodeBeside Type="Sitecore.Shell.Controls.RichTextEditor.InsertImage.InsertImageForm,Sitecore.Client">

with



<CodeBeside Type="[YOUR_CLASS_NAME],[YOUR_ASSEMBLY]"/>


Note: I usually prefer to copy and paste the XML Layout into /sitecore/shell/override folder instead of modifying the existing one.

And now we can safely add our SVG Image !

Media Library Uploaded

But there is still a problem, which leads me to:

Problem 2: My Rich Text Editor is not handling the SVG correctly !

So now we need to make the image appear ! The problem shows itself when we check the html of the Rich Text Editor.

SVG Image

The SVG image is handled as a normal Sitecore Media File, meaning that it goes through the image handler and is displayed as a normal image in <img> tag. The problem is that the SVG is actually a XML File which needs to be rendered as it is for consistency. If your FrontEnd developers don’t want to manipulate the actual SVG when it gets rendered – you can stop reading here🙂. If you are one of the few unlucky ones – no worries, there is hope ! The salvation is again in the OnOK method of the InsertImageForm class, just a line bellow our modifications. If the image is considered a valid image file the following code gets executed:


MediaUrlOptions shellOptions = MediaUrlOptions.GetShellOptions();
shellOptions.Language = this.ContentLanguage;
string text = !string.IsNullOrEmpty(HttpContext.Current.Request.Form["AlternateText"]) ? HttpContext.Current.Request.Form["AlternateText"] : mediaItem.Alt;
Tag image = new Tag("img");
this.SetDimensions(mediaItem, shellOptions, image);
image.Add("Src", MediaManager.GetMediaUrl(mediaItem, shellOptions));
image.Add("Alt", StringUtil.EscapeQuote(text));
image.Add("_languageInserted", "true");
if (this.Mode == "webedit")
{
    SheerResponse.SetDialogValue(StringUtil.EscapeJavascriptString(image.ToString()));
    base.OnOK(sender, args);
}
else
    SheerResponse.Eval("scClose(" + StringUtil.EscapeJavascriptString(image.ToString()) + ")");

So no matter the image type – it always gets appended to an <img> tag with the corresponding URL. In order to make our SVG fully functional instead of rendering the image itself we need to render the XML contents of the actual media file. It can be done easily by reading the stream of the actual media image. Also we need to make sure that we set the width and height in which we want to display our SVG. The modified code looks like this:


if (mediaItem.MimeType == "image/svg+xml")
{
   string result;

   using (StreamReader reader = new StreamReader(mediaItem.GetMediaStream(), Encoding.UTF8))
   {
      result = reader.ReadToEnd();
   }

   XDocument svg = XDocument.Parse(result);
   NameValueCollection form = HttpContext.Current.Request.Form;

   if (svg.Document?.Root != null)
   {
      int width;

      if (int.TryParse(form["Width"], out width))
      {
         svg.Document.Root.SetAttributeValue("width", width);
      }

      int height;

      if (int.TryParse(form["Height"], out height))
      {
          svg.Document.Root.SetAttributeValue("height", height);
      }

      result = svg.ToString();
   }

   if (Mode == "webedit")
   {
      SheerResponse.SetDialogValue(StringUtil.EscapeJavascriptString(result));
      base.OnOK(sender, args);
   }
   else
   {
      SheerResponse.Eval("scClose(" + StringUtil.EscapeJavascriptString(result) + ")");
   }

}
else
{
   Tag image = new Tag("img");
   SetDimensions(mediaItem, shellOptions, image);
   image.Add("Src", MediaManager.GetMediaUrl(mediaItem, shellOptions));
   image.Add("Alt", StringUtil.EscapeQuote(text));
   image.Add("_languageInserted", "true");

   if (Mode == "webedit")
   {
      SheerResponse.SetDialogValue(StringUtil.EscapeJavascriptString(image.ToString()));
      base.OnOK(sender, args);
   }
   else
   {
        SheerResponse.Eval("scClose(" + StringUtil.EscapeJavascriptString(image.ToString()) + ")");
   }
}

By doing this modification – now the SVG is rendered as the xml it should be rendered as !🙂

SVG Rendered

So up to now we can properly render SVG Images in our Rich Text Editor ! But

Problem 3: But what about rendering it on a Sublayout/Rendering ?

Well, the next struggle is to render the SVG correctly in our components. And here is one of the things I love most about Sitecore – all field renders (if used correctly :)) goes through the RenderField pipeline ! Meaning – we just need to modify the GetImageFieldValue processor with our own and we should be set !

The GetImageFieldValue processor renders the image thanks to ImageRenderer helper class. So we need to create our own ImageRenderer which is going to support SVGs as well.

We are going to create a new class named ImageRendererEx which will contain all the code from the original ImageRenderer, but have modifications for rendering our SVG images. The only change here should be in the Render() method. The modified Render function looks like this:


public virtual RenderFieldResult Render()
{
   var obj = Item;
   if (obj == null)
   {
      return RenderFieldResult.Empty;
   }

   var keyValuePairs = Parameters;

   if (keyValuePairs == null)
   {
      return RenderFieldResult.Empty;
   }

   ParseNode(keyValuePairs);

   var innerField = obj.Fields[FieldName];

   if (innerField != null)
   {
      imageField = new ImageField(innerField, FieldValue);

      ParseField(imageField);
      AdjustImageSize(imageField, scale, maxWidth, maxHeight, ref width,
      ref height);

      if (imageField.MediaItem != null)
      {
         MediaItem imageMediaItem = new MediaItem(imageField.MediaItem);

         if (imageMediaItem.MimeType == "image/svg+xml")
         {
            string result;

            using (StreamReader reader = new StreamReader(imageMediaItem.GetMediaStream(), Encoding.UTF8))
            {
               result = reader.ReadToEnd();
            }

            XDocument svg = XDocument.Parse(result);

            if (svg.Document?.Root != null)
            {
               if (width > 0)
               {
                  svg.Document.Root.SetAttributeValue("width", width);
               }

               if (height > 0)
               {
                  svg.Document.Root.SetAttributeValue("height", height);
               }

               result = svg.ToString();
            }

            return new RenderFieldResult(result);
         }
      }
   }

   var site = Context.Site;

   if ((string.IsNullOrEmpty(source) || IsBroken(imageField)) && site != null &&
site.DisplayMode == DisplayMode.Edit)
   {
      source = GetDefaultImage();
      className += " scEmptyImage";
      className = className.TrimStart(' ');
   }

   if (string.IsNullOrEmpty(source))
   {
      return RenderFieldResult.Empty;
   }

   var imageSource = GetSource();
   var stringBuilder = new StringBuilder("<img "); AddAttribute(stringBuilder, "src", imageSource); AddAttribute(stringBuilder, "border", border); AddAttribute(stringBuilder, "hspace", hspace); AddAttribute(stringBuilder, "vspace", vspace); AddAttribute(stringBuilder, "class", className); AddAttribute(stringBuilder, "alt", HttpUtility.HtmlAttributeEncode(alt), xhtml); if (width > 0)
   {
   AddAttribute(stringBuilder, "width", width.ToString());
   }

   if (height > 0)
   {
   AddAttribute(stringBuilder, "height", height.ToString());
   }

   CopyAttributes(stringBuilder, keyValuePairs);
   stringBuilder.Append(" />");
   return new RenderFieldResult(stringBuilder.ToString());
}

And we are done ! Now no matter if we are rendering the image in a MVC Rendering or WebForms Sublayout, if we are using the Sitecore Helper or the Web Control (Worth to mention that it will also work if you are using ORM as long as it is using the RenderField pipeline to fill the values – like Glass Mapper is :)) we will get a nicely formatted SVG image!

Field Rendered Image

 

Full code can be found on BitBucket.

Happy SVGing !🙂

Sitecore Custom Workflow Email Action

Workflows are one of the most powerful tools in the Sitecore toolkit. An interesting request we recently had was to build a custom workflow action that will send email to a different mailing group/groups based on the location and the language of the item moved to next step of the workflow. At first it seemed like a complex task but as in many cases – Sitecore is pretty extensible. In this blog post I will cover the simplest possible implementation where we will just send the email to a different mailing group or email. The build should be pretty extensible, so feel free to extend based on it. There are three main steps required for the component implementation:

  1. Create set of templates that will allow the users to control the rules.
  2. Create a new save action template that will build on top of the existing email action.
  3. Code it🙂

Rule Engine (Kind of :))

For the rule engine we will need 2 templates – one folder template will just store our rules and one rule template. The rule item should give the administrators a chance to pick a start path(from where the items will count as a members of the section) and a possibility to pick languages in which the approvers should be able to move the the item to the next step of the workflow, and finally a list of emails. To be consistent, we are going to add the templates to /sitecore/templates/System/Workflow

So here is an example of how the rule template should look like:

Rule Template

The next decision we have to make is where to place the rules in the content tree. I usually don`t like adding items to the /sitecore/system node if it is not absolutely necessary, but it seems like a good location to place them. Another good option is to place them under the settings node of the website (if there is one). In this example I will place them under the /sitecore/system node like this:

Workflow Rules

Save Action Template

The EmailActionEx should be similar to the existing sitecore/templates/System/Workflow/Email action. The only addition is adding our custom parameter, so the user will be able to pick rules for the action. The save action should look like this:

Email Action Ex

Keep in mind that the Rules DataSource might vary based on where you decided to place your rules folder.

Code Time !🙂

When coding the custom action we need to make sure that we keep the existing token functionality of the default functionality of the existing save action (like tokens etc). Snippet of the source code can be found below:


using System;
using System.Collections.Generic;
using System.Linq;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using System.Net.Mail;
using Sitecore.Globalization;
using Sitecore.Workflows.Simple;
 
namespace Sitecore.Workflows.Web.Workflows
{
    public class SendEmailExAction
    {
        public void Process(WorkflowPipelineArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
 
            ProcessorItem processorItem = args.ProcessorItem;
 
            if (processorItem == null)
            {
                return;
            }
 
            Item innerItem = processorItem.InnerItem;
 
            string fullPath = innerItem.Paths.FullPath;
 
            List<string> recipents =
                GetItems(innerItem, "rules")
                    .Where(x => ShouldSendEmail(args.DataItem, x))
                    .SelectMany(x => x.Fields["Emails"].Value.Split(';'))
                    .ToList();
 
            if (recipents.Any())
            {
 
                string from = GetText(innerItem, "from", args);
                string mailServer = GetText(innerItem, "mail server", args);
                string subject = GetText(innerItem, "subject", args);
                string message = GetText(innerItem, "message", args);
 
                Error.Assert(from.Length > 0, "The 'From' field is not specified in the mail action item: " + fullPath);
                Error.Assert(subject.Length > 0,
                    "The 'Subject' field is not specified in the mail action item: " + fullPath);
                Error.Assert(mailServer.Length > 0,
                    "The 'Mail server' field is not specified in the mail action item: " + fullPath);
 
                MailMessage mailMessage = new MailMessage();
 
                mailMessage.From = new MailAddress(from);
                mailMessage.Subject = subject;
                mailMessage.Body = message;
 
                foreach (string recipent in recipents)
                {
                    mailMessage.To.Add(new MailAddress(recipent));
                }
 
                SmtpClient client = new SmtpClient(mailServer);
 
                try
                {
                    client.Send(mailMessage);
                }
                catch (Exception ex)
                {
                    Log.Error("EmailExAction Threw An Exception", ex, this);
                }
            }
        }
 
        ///

<summary>
        /// Gets the text.
        /// 
        /// </summary>


        /// <param name="commandItem">The command item.</param><param name="field">The field.</param><param name="args">The arguments.</param>
        /// <returns/>
        private string GetText(Item commandItem, string field, WorkflowPipelineArgs args)
        {
            string text = commandItem[field];
            return text.Length > 0 ? ReplaceVariables(text, args) : string.Empty;
        }
 
        ///

<summary>
        /// Replaces the variables.
        /// 
        /// </summary>


        /// <param name="text">The text.</param><param name="args">The arguments.</param>
        /// <returns/>
        private string ReplaceVariables(string text, WorkflowPipelineArgs args)
        {
            text = text.Replace("$itemPath$", args.DataItem.Paths.FullPath);
            text = text.Replace("$itemLanguage$", args.DataItem.Language.ToString());
            text = text.Replace("$itemVersion$", args.DataItem.Version.ToString());
            return text;
        }
 
        private bool ShouldSendEmail(Item dataItem, Item ruleItem)
        {
            return IsItemMatch(dataItem, ruleItem) && IsLanguageMatch(dataItem, ruleItem);
        }
 
        private bool IsItemMatch(Item dataItem, Item ruleItem)
        {
            Item ruleStartItem = GetStartItem(ruleItem);
 
            return ruleStartItem == null || IsAncestor(dataItem, ruleStartItem);
        }
 
        private bool IsLanguageMatch(Item dataItem, Item ruleItem)
        {
            List<Language> languages = GetLanguages(ruleItem).ToList();
 
            return !languages.Any() || languages.Contains(dataItem.Language);
        }
 
        private IEnumerable<Language> GetLanguages(Item ruleItem)
        {
            MultilistField selectedLanguages = ruleItem.Fields["Languages"];
 
            if (selectedLanguages != null && selectedLanguages.TargetIDs.Any())
            {
                return selectedLanguages.GetItems().Select(x => Language.Parse(x.Name));
            }
 
            return Enumerable.Empty<Language>();
        }
 
        ///

<summary>
        /// Gets the items
        /// </summary>


        /// <param name="commandItem"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        private IEnumerable<Item> GetItems(Item commandItem, string field)
        {
            MultilistField rules = commandItem.Fields[field];
 
            if (rules != null && rules.TargetIDs.Any())
            {
                return rules.GetItems();
            }
 
            return Enumerable.Empty<Item>();
        }
 
        private Item GetStartItem(Item ruleItem)
        {
            ReferenceField startPathField = ruleItem.Fields["StartPath"];
 
            if (startPathField != null && startPathField.TargetItem != null)
            {
                return startPathField.TargetItem;
            }
 
            return null;
 
        }
 
        private bool IsAncestor(Item currentItem, Item ancestor)
        {
            if (currentItem.ID == ancestor.ID)
            {
                return true;
            }
 
            if (currentItem.Parent != null)
            {
                return IsAncestor(currentItem.Parent, ancestor);
            }
 
            return false;
        }
    }
}

In the code we need to check if the item matches both rules for language and location. If one or more of the rules are matched – we send the email to the corresponding emails. Otherwise we skip them. If there is no match – no email is sent.

And that is it ! Now we have our custom email action that can send items based on the item location and the item language !

The Sample Project can be found on BitBucket.

For people without TDS the package can be downloaded separately from here: SitecoreWorkflows-1.0.zip

Happy Workflowling !

The Amazing World of RAZL Part 2 – Script Like A Boss

It has been almost a year from my first post about The Amazing World of Razl and the second part finally found its place in the posts pipeline. In this post I am going to dig deeper into one of the most amazing and useful Razl features – Scripting. Also for the brave ones who reach the end of the article there will be a sneak peak of the new Razl version!🙂

First, let’s go through a real life scenario that every single Sitecore developer faced at some point. Many times we find ourselves slammed with bugs, which are caused by differences in content in our QA and Production Instances. So every single time we need to explain that our QA instance is not up to date with the latest Production content and that it is normal to have these issues. After that the frequent scenario is – “Can you please update the content, so we can regression test feature X”. Following that, we need to go to our Production CM, export a content package, and install it on our QA instances. The huge problem, with this, is that it happens pretty frequently. Especially on big projects with many content dependencies (i.e. I want to test the search boost I requested but I get only 2 search results in total). This is the case in which the Razl Scripting mode shines.

Razl scripts are actually a fairly simple xml files which can be executed from the command line. They have 2 main components – connections and operations.

Connections

Connections are actually a way to describe a Sitecore Instance that Razl needs to connect to. The basic XML for Razl looks like this:




<connection name="" readOnly ="" install="" preset="">



<url></url>



<accessGuid></accessGuid>



<database></database>



<path></path>



</connection>



So let’s explore what the different parameters and attributes do:

  • name – This is the main identifier that is going to get used by the operations to determine the Instance they are going to execute on.
  • readOnly – The possible values are true and false. Specifies if the connection is read only, i.e. if we try to execute a write command on a read only connection – it will not get executed. It is a good practice in your scripts to set your connection to read only.
  • install– The possible values are again true or false. Specifies if the script should install the Razl Service if it is not already installed. If set to true – the path parameter must be specified.
  • url– Used to define the url of the Sitecore Instance we want to connect to.
  • accessGuid– Defines the access Guid which is used by Razl to access the Sitecore Instances. If Razl is already installed on the Sitecore Instance – the Guid can be obtained by reading it`s value from the web.config defined in [Webroot]\_CMP.
  • database– The database which is going to be used by the connection. Nothing fancy here.
  • path– The UNC path to the Sitecore instances. It is used only when you want to install the Razl Service on the server. My recommendation is to preinstall the Razl Service on all servers you want to script on, so you won`t have to expose your servers file systems.
  • preset– If you already have the connection in the RAZL desktop application and you are too lazy to write some xml – you can use the preset property to pass the name as it is in your RAZL application. I usually advice against that, because you cannot be sure which presets are actually installed on the server and in most cases you won`t be the only person using the script.

There is no upper limit on how many connections you can define and naturally, you should have at least 2 connections defined in the script – because you are going to use it on at least 2 Instances (or 2 Databases of a single instance).

Operations

Operations are the way of script telling RAZL what needs to be executed. The basic operation XML looks like this:




<operation name="" source="" target="">

<parameter name="param">

<value></value>

...

</parameter>

</operation>



So let’s take a look at what the different attributes do:

  • source– The name of the connection you want to use for sourcing Sitecore Instance/Database Combo (Copy From :))
  • target– The name of the connection you want to use for targeted Sitecore Instance/Database Combo (Copy To :))
  • name– Name is actually used for the operation types – CopyItem, CopyAll and CopyHistory. I will cover what each of the 3 operation types do later in this post as we need to get familiar with the parameters first🙂

Every operation can have one or more parameters. All parameters have a name attribute, every operation type has a list of supported parameter names. Parameters themselves can be single valued and multi-valued.

Here is an example of a

  1. Single valued parameter xml:

<parameter name="">[value]</parameter>

  1. Multi-valued parameter



<parameter name="">

<value>[value]</value>

<value>[value]</value>

.....

</parameter>



As different operation types accept different types of parameters, I will describe them in a separate section.

Operation Types

Copy Item Operation

Base Syntax:




<operation name="CopyItem" source="" target="">



<parameter name="itemId"></parameter>



</operation>



Functionality:

The Copy Item operation copies a single item from the source to the target database. It requires one parameter – itemId which is the ID of the item in the source database. Nothing fancy here.

Copy All Operation

Base Syntax:




<operation name="CopyAll" source="" target="">



<parameter name="itemId"></parameter>



<parameter name="overwrite"></parameter>



</operation>





Functionality:

The Copy All operation copies an item and its descendants from the source to the target database. It requires 2 parameters – itemId which is the ID of the starting item in the source database from which the copying starts and overwrite is a parameter which determines what to do with items which are present in the target database, but are missing in the source database, i.e. if the setting is set to true and there is an item in the target database that is not present in the source – it will get deleted, if the setting is set to false – the item will be skipped.

Copy History Operation

Base Syntax:


<operation name="CopyHistory" source="" target="">



<parameter name="from"></parameter>



<parameter name="to"></parameter>



<parameter name="recycle"></parameter>



<parameter name="include">



<value></value>



</parameter>



<parameter name="exclude">



<value></value>



</parameter>



</operation>



Functionality:

I am sure many of you are familiar that Sitecore comes with a very powerful History Engine. The history engine uses a SQL Table to store all events that happened to an item. The history engine is enabled by default in the recent releases of Sitecore, before that you needed to enable that manually from the web.config.

The Razl copy history operation will read the actions from this table in the source database which occurred in a certain time frame and execute them over the target database. As this operation is more risky:) it accepts several parameters to give you better control over what is going on.

  • The “from” parameter specifies the start time from which Razl should start reading events from the history table. The date needs to be in the following format: yyyy-MM-ddThh:mm:ss. This is the only required parameter of the operation.
  • The “to” parameter specifies the end time when Razl should stop reading events from the history table. The date format is the same like the one for the “from” parameter: yyyy-MM-ddThh:mm:ss. Parameter is optional and if it is not set every event up to now will be executed.
  • The “recycle” is a boolean parameter and it specifies what Razl should do with deleted items. If set to true it will move them to recycle bin and if set to false it will remove them (they will just be deleted not recycled). I strongly advise of always setting this parameter to true as you cannot be sure what the content editors might have done. The parameter is optional with a default value of true.
  • The other two parameters (includeand exclude) are multi-valued and control which items (and their descendants) should be included/excluded when performing events. Both parameters work with item paths and exclude has bigger power over include (in case a path is included in both parameters). If the parameters are left blank – all actions will get executed. If there is a single path in the include – only actions over this item and its descendants will be executed.

I also created a small set of scripts which can be used for a good starting point when you are writing your own. The scripts can be found on BitBucket.

RAZL 2.6

And now – some spoilers!🙂

There is a new release of Razl coming soon!🙂

With the new release there are several new features, along with huge improvements to the scripting mode! Here are some of the highlights of the new release!

Lightning Mode

Razl 2.6 will introduce a new Lightning Mode for comparison. It increases comparison speed dramatically by using only the revision number when comparing!

Deep Compare

One of the most requested Razl features, deep compare will ship with the new release! Now you will be able to deep compare items without the need to go down the tree !

Scripting Mode Improvements

Razl 2.6 will introduce 5 new operation types:

  1. CopyVersion – For copying a specific version of an item.
  2. DeleteItem – For deleting items.
  3. MoveItem – For moving items in the content tree.
  4. SetFieldValue – For setting field values from a Razl Script.
  5. SetPropertyValue – For setting property values !

Also – a new parameter lightningMode will be introduced for CopyAll and CopyHistory operations to use the new lightning mode for quick comparison of huge trees.

Make sure you stay up to date with the new Razl Features by

Razl Website: http://razl.net/

Hedgehog Development Website: http://www.hhogdev.com/

Our Twitter: @hhogdev

Happy Razling !

Permissions Required To Edit Rendering Parameters

Recently we faced a problem in which we needed a role that had to be able to edit the content and the presentation details of an item in the content tree. For authoring usually the sitecore\Author is the way to go (if you don`t have any specific multi-site or language specific rules) and the obvious choice for designing is to use the built-in sitecore\Designer role. But as it seems the designer role has Read, Write, Rename, Create and Delete permissions on almost everything which is located under the /sitecore/layout and /sitecore/templates folder as it can be seen on the screenshot below.

Sitecore Designer Permissions

This was a bit too much for our needs as we were not going to use this role for creating and designing renderings or setting presentation details on template level, but we needed it just to be able to edit the presentation details of an item.

The next possible resolution was to use the sitecore\Sitecore Client Designing (or give our custom role the necessary permissions to access the designer tab and edit the sublayouts) as it already had permissions to edit the presentation details of an item. Everything seemed to work fine until we tried to set the datasource of a sublayout in the Content Manager and saw that the role lacks the necessary permissions to edit the parameter template fields.

Parameter Template No Permissions

The first confusing part was that the role had permissions on the Caching section – which might actually be a bug in Sitecore.

The second confusing part was that the role had Field Read and Field Write access on the affected fields as seen on the screenshot below:

Field Write Enabled

So after some deep diving in the Sitecore.Client and the Sitecore.Kernel Assemblies I found that the actual rendering of the field happens in the RenderField Method of the EditorFormatter class.

 

    public virtual void RenderField(System.Web.UI.Control parent, Editor.Field field, bool readOnly)
    {
      Assert.ArgumentNotNull((object) parent, &amp;amp;amp;amp;amp;amp;quot;parent&amp;amp;amp;amp;amp;amp;quot;);
      Assert.ArgumentNotNull((object) field, &amp;amp;amp;amp;amp;amp;quot;field&amp;amp;amp;amp;amp;amp;quot;);
      Sitecore.Data.Fields.Field itemField = field.ItemField;
      Item fieldType = this.GetFieldType(itemField);
      if (fieldType == null)
        return;
      if (!itemField.CanWrite)
        readOnly = true;
      this.RenderMarkerBegin(parent, field.ControlID);
      this.RenderMenuButtons(parent, field, fieldType, readOnly);
      this.RenderLabel(parent, field, fieldType, readOnly);
      this.RenderField(parent, field, fieldType, readOnly);
      this.RenderMarkerEnd(parent);
    }

The moment when it is decided if the field can be modified is the check for itemField.CanWrite. Which leads to:

    public bool CanWrite
    {
      get
      {
        if (AuthorizationManager.GetAccess((ISecurable) this, (Account) Context.User, AccessRight.FieldWrite).Permission == AccessPermission.Deny)
          return false;
        if (this.ID == FieldIDs.Security || this.ID == FieldIDs.InheritSecurity)
          return this.Item.Access.CanAdmin();
        return this.Item.Access.CanWrite();
      }
    }

As can be seen from the code above the access for fieldwrite is taken into account is only when the permission is set to Deny. So the actual check that is happening is if there is write access on the current item (which is actually the _standard values of the /sitecore/templates/System/Layout/Rendering Parameters/Standard Rendering Parameters template).

All that said, after we gave write permissions on the _standard values of the standard rendering paramaters template – everything started working !

TL; DR  :)

Give Write Access to /sitecore/templates/System/Layout/Rendering Parameters/Standard Rendering Parameters/_Standard Values

WFFM Session Aware Single-Line Text Field

Recently we faced an issue where a value of certain Single-Line Text Field in Web Forms For Marketers had to be populated by a variable in the session. The problem is that WFFM only supports query string parameters for populating fields. The good thing is that WFFM is extremely easy to extend🙂. The tricky part here is that with the recent versions of Sitecore the field needs to be created for MVC as well (WFFM 2.4+ and Sitecore 7+). In addition to the field there will be two boolean values which will control if the field is going to be hidden (display:none;) and if the session variable should be removed after the field is populated (Which is easily doable with decorators).

So all in all building a session aware Single-Line Text Field requires 2 classes – 1 Main where our properties will be for WebForms and 1 for MVC. It is also required to register the field with WFFM which means creating a custom Field Item.

First goes the main class. It should have 3 Properties:

  1. SessionKey for storing the key which should be captured.
  2. DeleteKeyAfterCapture – flag which indicates if the key needs to be removed from the session after the value is already used.
  3. IsHidden – flag which indicates if the field needs to be hidden or displayed.

In WFFM the type of the field which will be displayed in the form designer is controlled by the VisualFieldType decorator. The problem here is that the Boolean Field type (not counting the tag field for Engagement Analytics :)) is represented by <select> element with two options – Yes and No. In the code there is a workaround for this problem taken from the DropList field. Here is the code for the class:

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web;
using Sitecore.Form.Core.Attributes;
using Sitecore.Form.Web.UI.Controls;

namespace WFFM.Custom.Fields
{
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public class SingleLineSessionTextField : SingleLineText
    {
        [VisualCategory("Session")]
        [VisualProperty("Session Key", 100), DefaultValue("")]
        public string SessionKey { get; set; }

        private bool _deleteKeyAfterCapture = true;

        [DefaultValue("Yes")]
        [VisualCategory("Session")]
        [VisualFieldType(typeof (Sitecore.Form.Core.Visual.BooleanField))]
        [VisualProperty("Delete Key After Capture?", 200)]
        public string DeleteKeyAfterCapture
        {
            get
            {
                return _deleteKeyAfterCapture ? "Yes" : "No";
            }
            set
            {
                _deleteKeyAfterCapture = value == "Yes";
            }
        }

        private bool _isHidden = true;

        [VisualCategory("Session")]
        [VisualFieldType(typeof (Sitecore.Form.Core.Visual.BooleanField))]
        [VisualProperty("Is Hidden?", 300), DefaultValue("Yes")]
        public string IsHidden
        {
            get
            {
                return _isHidden ? "Yes" : "No";
            }
            set
            {
                _isHidden = value == "Yes";
            }
        }

        protected override void OnLoad(System.EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(SessionKey))
            {
                if (HttpContext.Current != null && HttpContext.Current.Session[SessionKey] != null)
                {
                    Text = HttpContext.Current.Session[SessionKey].ToString();

                    if (_deleteKeyAfterCapture)
                    {
                        HttpContext.Current.Session.Remove(SessionKey);
                    }

                }
            }

            if (_isHidden)
            {
                Attributes["style"] = "display:none";
            }
        }
    }
}

The logic is located in the OnLoad method. There is basically a check for the Session Key and the field Text is prepopulated from the Session if the key exists – nothing complex🙂

Second goes the class for the MVC field which is only required if the field is going to be used in MVCForm Rendering.

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using Sitecore.Data.Items;

namespace WFFM.Custom.MVC.Fields
{
    public class SingleLineSessionTextField : Sitecore.Forms.Mvc.Models.Fields.SingleLineTextField
    {
        public string SessionKey { get; set; }

        [DataType(DataType.Text)]
        public override object Value { get; set; }

        public SingleLineSessionTextField(Item item)
            : base(item)
        {
            Initialize();
        }

        private void Initialize()
        {
            KeyValuePair<string, string> deleteKeyAfterCapture =
                ParametersDictionary.FirstOrDefault(x => x.Key.ToUpper() == "DELETEKEYAFTERCAPTURE");

            KeyValuePair<string, string> isHidden =
                ParametersDictionary.FirstOrDefault(x => x.Key.ToUpper() == "ISHIDDEN");

            if (!string.IsNullOrWhiteSpace(SessionKey))
            {
                if (HttpContext.Current != null && HttpContext.Current.Session[SessionKey] != null)
                {
                    Value = HttpContext.Current.Session[SessionKey].ToString();

                    if (deleteKeyAfterCapture.Value.ToUpper() != "YES")
                    {
                        HttpContext.Current.Session.Remove(SessionKey);
                    }

                    if (isHidden.Value.ToUpper() != "YES")
                    {
                        if (!string.IsNullOrWhiteSpace(CssClass))
                        {
                            CssClass += " hidden";
                        }
                        else
                        {
                            CssClass = "hidden";
                        }

                    }
                }
            }
        }
    }
}

As the properties are already present we can just reuse them. The problem is that the properties for the complex structures like the BooleanField cannot be handled directly and they need to be taken from the ParametersDictionary. In WFFM 2.5 there is an extension method for getting values from the ParametersDictionary(and any other dictionary basically :)). So it is possible to replace the KeyValue Pair selections with the following if the target version is 2.5:


string deleteKeyAfterCapture = Sitecore.Forms.Mvc.Extensions.DictionaryExtensions.GetValue(this.ParametersDictionary, "deletekeyaftercapture");
string isHidden = Sitecore.Forms.Mvc.Extensions.DictionaryExtensions.GetValue(this.ParametersDictionary, "ishidden");

The only thing remaining is registering the field with WFFM. This is achieved by creating an item of from the Field Type Template under /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types. (My personal preference is to place them under /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types/Custom, but any other folder will do it).

The main values that need to be populated are:

  1. Assembly – The assembly in which the Main (WebForms) class is located.
  2. Class – Fully Qualified Class Name of the Main (WebForms) class.
  3. MVC Type – Required only if the field is going to be used in MVCForm Rendering. Must be in the following format – [FullyQualifiedClassName], [AssemblyName]

Here is an example with the values set:

WFFM Field

And it is done ! The field type is now selectable in the form designer and the properties are edited from the left hand menu, as it can be seen on the screenshot bellow!

Form Designer

The full code for the field can be found on BitBucket .

 

Sitecore Adaptive Images – Huge Crawling Logs Problem

Recently we noticed that on our production environment the Crawling Log files were abnormally big – between 500 MB and 1 GB. After some digging we found out that they were just filling up with exceptions while crawling the media library. The exceptions look like this:


4576 10:48:07 WARN Could not compute value for ComputedIndexField: urllink for indexable: sitecore://master/{8EA15044-EE2C-41DB-81D6-0A9C42062814}?lang=en&ver=1
Exception: System.NullReferenceException
Message: Object reference not set to an instance of an object.
Source: Sitecore.Kernel
at Sitecore.Context.PageMode.get_IsNormal()
at adaptiveImages.AdaptiveImagesMediaProvider.GetMediaUrl(MediaItem item)
at Sitecore.ContentSearch.ComputedFields.UrlLink.ComputeFieldValue(IIndexable indexable)
at Sitecore.ContentSearch.LuceneProvider.LuceneDocumentBuilder.AddComputedIndexFields()

Considering the case that we have tons of media (over 2000 items) and languages (there is a separate exception for each language) we had some pretty serious index time performance issues and hard disk space problems.

After some debugging we found out that the Sitecore.Context.PageMode.IsNormal was throwing null reference. The huge problem was that there is no way to protect against null pointers here because you cannot check the Sitecore.Context and the Sitecore.Context.PageMode for nulls. The good part is that the PageMode.IsNormal method code is using the Sitecore.Context.Site (which is the actual null in this case) to check the PageMode.


public static bool IsNormal
{
    get
    {
        return Context.Site.DisplayMode == DisplayMode.Normal;
    }
}

With this information the fix became pretty trivial. As try-catch is pretty expensive operation – we just needed to check if the Context.Site is not null and the Context.Site.DisplayMode is normal. The fixes took place in the two GetMediaUrl overrides. Here is the code for the modified methods which were causing the exception.

  /// <summary>
        /// Gets a media URL.
        /// </summary>
        /// <param name="item">The media item.</param>
        /// <returns>
        /// The media URL.
        /// </returns>
        public override string GetMediaUrl(MediaItem item)
        {
            Assert.ArgumentNotNull(item, "item");

            // FIX FOR CHECKING THE PAGE MODE
            bool isNormal = Context.Site != null && Context.Site.DisplayMode == DisplayMode.Normal;

            //If media item is not an image or the page context is not normal, then return
            if (!IsImage(item) || !isNormal)
                return base.GetMediaUrl(item);

            MediaUrlOptions mediaUrlOptions = new MediaUrlOptions();

            return GetMediaUrl(item, mediaUrlOptions);
        }

        /// <summary>
        /// Gets the media URL.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="mediaUrlOptions">The media URL options.</param>
        /// <returns></returns>
        public override string GetMediaUrl(MediaItem item, MediaUrlOptions mediaUrlOptions)
        {
            Assert.ArgumentNotNull(item, "item");
            Assert.ArgumentNotNull(mediaUrlOptions, "mediaUrlOptions");

            // FIX FOR CHECKING THE PAGE MODE
            bool isNormal = Context.Site != null && Context.Site.DisplayMode == DisplayMode.Normal;

            //If media item is not an image or the page context is not normal, then return
            if (!IsImage(item) || !isNormal || Context.Database == null || Context.Database.Name != _database)
                return base.GetMediaUrl(item, mediaUrlOptions);

            //If resolution cookie is not set
            if (!IsResolutionCookieSet())
            {
                //If mobileFirst is set to FALSE or user agent is identifying as a desktop, return with largest break-point resolution
                if (!_mobileFirst || IsDesktopBrowser())
                {
                    mediaUrlOptions.MaxWidth = GetLargestBreakpoint();
                    return base.GetMediaUrl(item, mediaUrlOptions);
                }
                //Return with mobile-first breakpoint (Smallest)
                mediaUrlOptions.MaxWidth = GetMobileFirstBreakpoint();
                return base.GetMediaUrl(item, mediaUrlOptions);
            }

            // If Max-width is not set or Max-width is greater than the selected break-point, then set the Max-width to the break-point
            if (mediaUrlOptions.MaxWidth == 0 || mediaUrlOptions.MaxWidth > GetScreenResolution())
                mediaUrlOptions.MaxWidth = GetScreenResolution();

            // If Max-width is not set and the 'maxWidth' setting is not empty, then set the Max-width property to the maxWidth
            if (mediaUrlOptions.MaxWidth == 0 && !string.IsNullOrEmpty(_maxWidth))
            {
                int maxWidth = 0;
                if (int.TryParse(_maxWidth, out maxWidth))
                {
                    // If pixel ratio is normal
                    if (GetCookiePixelDensity() == 1)
                        mediaUrlOptions.MaxWidth = maxWidth;
                    else
                        mediaUrlOptions.MaxWidth = maxWidth * GetCookiePixelDensity();
                }

            }

            return base.GetMediaUrl(item, mediaUrlOptions);
        }

Basically we are using an isNormal variable to check the Context.Site display mode instead of using the Context.PageMode.IsNormal property:


            bool isNormal = Context.Site != null && Context.Site.DisplayMode == DisplayMode.Normal;

By doing this we kept the module’s consistency and logic as we just added a null check for the Context.Site. After the fix our logs became around 2 kb again and our indexing speed came back to normal !

Happy Log Fixing !🙂