Tag Archives: sitecore experience editor

Sitecore 8 – Create a custom personalization rule

Sitecore OOTB comes with tons of personalization rules that we can use, however there might be times where we require to create a custom rule condition that fits to the client business requirement.

Luckily with Sitecore framework that’s known easy to extend, we can easily achieve this.

Here’s the steps to create a custom personalization rule:

1.Register the tag

First of all we need to create a new tag item under /sitecore/system/Settings/Rules/Definitions/Tags

create-custom-personalization-rule-create-tag2. Register a new rule element

Create a new element folder in /sitecore/system/Settings/Rules/Definitions/Elements

create-custom-personalization-rule-create-element-folder3. Create a new personalization condition rule

create-custom-personalization-rule-create-personalization-rule

Here I’m creating a new rule and calling it “Specific Template Name” which will do what exactly that, checking if the current context item is based on a specific template name – not really useful in real world scenario but this post is about how to setup a custom personalization rule 🙂

In the newly created rule we need to set the Text and Type fields

Text field contains the text that we’re going to present to the content author, here’s the following text that I use

the [TemplateName, StringOperator,,compares to] represents the format of input that we want to get from the content author. It follows the following format

  • OperatorId, defines the public property of the class where we want to assign the value coming from the content author input
  • StringOperator, the built-in macro that we want to use to get the input from the user. In this case this will be a string comparation operation
  • blank, this parameter will depend on the type of macro that we use, this could be a default text value if we are using the default macro,  it could be a default start path if we’re using the Tree macro  – think of it like setting a field source when we’re building a template in Sitecore. A full list of macros can be found in /sitecore/system/Settings/Rules/Definitions/Macros
  • compares to, the last parameter is the text representation that we want to show to the content author, this value is clickable and when clicked Sitecore will display the appropriate input control based on the macro that we set on the second parameter

The Type field is the full assembly name of the custom class that we want to use to perform the logic behind this personalization rule

4. Assign our custom element default tag definition to our custom tag

create-custom-personalization-rule-assign-element-tag-definition-to-tag

Of course we  can assign our custom element tag definition to one of the existing tag under /sitecore/system/Settings/Rules/Definitions/Tags so that it will automatically appear on the content author personalization rule window however if you want to make it obvious which one is your custom ones then I recommend creating your own custom tag and assign it to that

4. Assign the tag to Conditional Rendering

The last step to make the rule visible for the content author to choose from is to assign our custom tag to one of default Rules Context folder

create-custom-personalization-rule-assign-tag

From the picture we’re setting the tag to the Conditional Rendering rules context which will appear when the user want to personalize a certain component, but you can also some other Rules Context folders such as FXM ones.

After we assign the tag, we can verify that the custom personalization rule is available for the content author to choose from

create-custom-personalization-rule-verifying-the-custom-rule-appears

Here’s the class that’s responsible to evaluate the custom rule condition

The class logic is simple and not doing much for the purposes of the tutorial. In a real world scenario you can make a rule that reads from a custom database, read a configuration file, call an external API (be careful with this as it will increase page load time).

This may sound like a lot of work at first compared to just creating a custom code in the rendering or similar approach. But when we consider that we’re enabling the content author/marketers to do it by themselves and removing/minimizing the dependency towards IT it would open more possibilities for the marketers of what they can do using the platform and lessen time to market.

 

Sitecore 8 adding edit meta data button in experience editor

Are you still making your content author to edit the content of a page using content editor? then stop.

For content author in Sitecore the best way for them to edit a page is to use Sitecore Experience Editor. This way they can focus on editing the content on the page with visual indication what would their changes would look like. However there are some cases where content can’t be edited on the page because they are not displayed on the page itself, Metadata information is such a case.

There are some guide already out there to set up an edit meta data button in page editor in Sitecore 7 and below, and here I would like to do the same in Sitecore 8. This is based on the guide that I’ve found on http://blog.istern.dk/2015/03/02/running-sitecore-field-editor-from-a-speak-command-in-sitecore-experience-editor/ which can be reused to create other type of functionality.

First you need to install Sitecore Rocks, because in Sitecore 8 the interface is built using SpeakUI you may find it quite different with how you usually set up a new button in Sitecore 7 and below. I’ve also found it easier to create a SpeakUI component using Sitecore Rocks, so you definitely should check it out. After you’ve installed Sitecore Rocks and setup a connection to the Sitecore website you should see the following screen.

sitecore-rocks

 

Expand the core database and go to sitecore->content->applications->webedit->ribbons->webedit

Here you can see a couple of familiar strips which is what being displayed on the experience editor

rocks-webedit

experience-editor-ribbons

We want to add a new button to edit meta data here, so go in Sitecore Rocks and under the WebEdit node follow this steps

  • Add a new Page Properties strip
    • Add a new item with the template of Strip.
    • Set the header to “Page Properties” and the ID to “PagePropertiesStrip”
    • In Sitecore Rocks select tasks and design layout then add a new rendering of type Strip
  • Add a new Hidden Content chunk
    • Under the new Page Properties strip add a new item with the template of Chunk
    • Set the header to “Hidden Content” and ID to “HiddenContentChunk”
    • In Sitecore Rocks select tasks and design layout then add a new rendering of type Chunk
  • Add a new Edit Meta data button
    • Under the new Hidden Content chunk
    • Set the header to “Edit Meta Data” and ID to “EditMetaDataButton”
    • In Sitecore Rocks select tasks and design layout then add a new rendering of type LargeButton
    • Edit the rendering properties of the LargeButton
      • Set AccessKey to Meta Keywords|Meta Description (this the meta data fields that I’ve setup in Sitecore)
      • Set the Click property to trigger:button:click
      • Set the Command to LaunchFieldEditor
      • Set the PageCodeScriptFileName to the path of the LaunchFieldEditor.js file is

rocks-editmetadatabutton-properties

What it currently looks like so far

rocks-editmetadatabutton-tree

experienceeditor-editmetadatabutton

At this stage we still need to register the command to handle the edit meta data button functionality so let’s go ahead and do that.

The GenerateFieldEditorUrl class

The LaunchFieldEditor.js

update: if you’re using 8.1 + then the js code needs to be modified slightly – feedback from Yoki in the comments (and tested in 8.2u1)

 

After all have been setup correctly the content author can now edit the meta data button through the experience editor.

experienceeditor-editmetadatabutton-result