Using RequireJs to manage script dependencies based on attributes in your Sitecore Solution

A common architectural issue I hear amongst developers is how to prevent Sitecore loading all and sundry when it comes to javascript dependencies. Indeed it is often the case that front end developers don’t inform us or assume that we will use a single page type model and therefore don’t really need to worry about the number of script dependencies. The reality is that in actual fact if we included every script that a front end developer used throughout all of the scripts within the site – we would have 20 odd script tags in our markup, something you may well be keen to avoid.

As a solution to this, myself and many other developers including former colleague and Sitecore Solution Architect Richard Seal in his post Using Require to Organize your Sitecore Javascript opt to use an advanced module loader such as RequireJs. If you haven’t used it before, I recommend first reading their documentation as there are definite ways to write your javascript (and in particular jQuery) code to maximise your success with it.

Continue reading

Sitecore WebAPI – Hooking into the route setup

Setting up routes in MVC and WebAPI is pretty straightforward in Sitecore provided that its done in the correct pipeline. When I first started attempting to add routes, I quickly discovered that doing this via the Global ASAX wasn’t reliable. The reason for this is that the routes are setup in the initialise pipeline.

Fortunately Sitecore has this process mapped out in the the config file so all I needed to do is swap out the DefaultRouteMapper that comes out of the box. Lets look at the entry in Sitecore:

<setting name="Sitecore.Services.RouteMapper" value="NAMESPACE.CustomRouteMapper, ASSEMBLY_NAME" />

Continue reading

Sitecore WebAPI – Setting up IOC

I’ve had the opportunity to tinker in the depths of the WebAPI in Sitecore 7.5 and 8 recently. I’ll put up a post about setting up controllers easily shortly but until then I thought I’d share how to setup IOC.

In a normal WebAPI environment, we can create our resolver and attach it to the GlobalConfiguration. However, we must remember that Sitecore uses its own pipelines for MVC and WebAPI and so setting this up in the Global ASAX won’t work.

The way I’ve done it is to change the registered WebAPI initialiser class in the Sitecore.Services.Config file found in App_Config/Include. This lets me hook directly into the entry point for Sitecore related WebAPI and adjust the configuration accordingly.

Continue reading

Sitecore 8 upgrade – check your .NET version

One of my current clients is making the transition to Sitecore 8. This went reasonably well as we decided to do a fresh installation rather than follow the upgrade path and then copy any content in manually.

What we weren’t prepared for was a number of errors using Sql Server and IIS. Specifically we were getting heap exceptions with and ntdll.dll. However, what was funny was that these errors weren’t occuring on everyones systems. In fact the only repeatable thing was sporadic database issues with connectivity.

After some investigation we found that we were on .NET 4.5.1 and this has some known issues. So if you’re getting issues with IIS, the worker process or with the dlls then this is one thing to look into.

Needless to say we did a test installation on a working system (well one with the least amount of issues) and this ran ok. Then we rolled it out to the other systems and this fixed our system. You can find out more about the list of known issues on the Microsoft support site.

The .Net 4.5.2 update is a web installer so you’ll need Internet access for any machine you’re updating.


Back to Basics: Sitecore validation – part 1

This is the first post in a series about back to basics. All to often I come across Sitecore projects that commit a number of sins. Hopefully this will tie together with the continuing series on working with the Sitecore platform instead of against it. So with that in mind, the first few parts will be about field validation in Sitecore.

Field validation is often overlooked. I think in part because the template editor has a quick and dirty interface for setting all the common properties such as name, source, shared etc. But to setup the validation, help text and the like you have to go into the field itself.

However, the content editors experience can be greatly enhanced with good validation with clear, concise error messages. It also means that as developers, we can enforce a little safety in our environment and increase our chances that our code will get data in the right ‘shape’.

A short history lesson

Before Sitecore 5, validation was done with regular expressions and a generic error message. This would fire and the user would see a popup with that message. After a while this would get pretty annoying as the editor couldn’t really see which fields were invalid and, in some cases, the warning wasn’t a critical error. Inevitably, unless all of the fields were required and the error messages could be made verbose enough, validation was turned off.

Even now Sitecore 7.x supports this functionality but using it is an incredibly bad idea.

Sitecore validation done right

Validation done right means you need to use the validators built into Sitecore (or add your own) using the various multi-lists directly on the field item for your template. If you do so, you have the advantage that as the editor builds their content, they get visual cues as to which field is incorrect. This appears as a red bar next to the field and colour spots to the top right (in the content editor). You also have the advantage that you can set the levels of these errors and class some as critical meaning the user can’t progress without fixing and you can make others ‘a bad idea’.

More importantly,Β these are all written in .NET code. What this means is that when you write your own, you’ll be able to unit test them under various conditions and more easily have them as part of your continuous integration builds.

In summary

This post was particularly short as an introduction to validators but they areΒ more of a ‘show me the code’ topic. Its also pretty near to Christmas and I have small people to entertain! πŸ™‚

In my next post on this topic, I’ll look at writing a very simple validator, registering it with Sitecore and setting its error level. More importantly, we’ll look at making it have different options per language and so handle some interesting scenarios. We’ll also have a unit test setup for it and I’ll put it on Github for reference.


Simple Sitecore Tip – Right Click > Preview & Other Actions

Carrying on from the little tips post on Right Click Publishing, I had a requirement to add a few extra context menu items. I will use ‘Preview’ as the example here.

In order to give a context preview, follow these steps:

  • Change to the Core database
  • Create an item /sitecore/templates/System/Menus/Menu item called ‘Preview’
  • Set up the data in the item to look like this: Preview
  • Save your item

You can do similar with most of the item: which can usually be found here /sitecore/content/Applications/Content Editor/Ribbons/Chunks.

That’s it! πŸ˜€

PS: Please note, sure these tips aren’t rocket science, but if it helps 1 person once, my work here is done πŸ˜€

Simple Sitecore Tip – Right click > Publish Item

Ok, so I really don’t know why I get so offended by this Sitecore behaviour, but I do. Why is it I cannot right click on an item and publish it? So here is a simple way of achieving this.

In the core database copy:

/sitecore/content/Applications/Content Editor/Menues/Publish/Publish Item

To here:

/sitecore/content/Applications/Content Editor/Context Menues/Default

Job Done πŸ˜€

PS: Please note, sure these tips aren’t rocket science, but if it helps 1 person once, my work here is done πŸ˜€

Datasources beneath the context item

This is possibly gonna be the shortest blog post I will ever do, but possibly one of the most useful tit-bits I have found in the Sitecore CMS. Ever wanted to force a user to select a datasource that is relative to the item its being used on ? I have LOADS of times, and after a bit of digging I found this:

Simply setting the Datasource Location to ./ (and I believe any relative path) for your renderings / sublayouts allows just this!

Every day is a (Sitecore) school day !! πŸ˜€

Creating custom permissions in sitecore

In this post I describe the process for creating custom permissions on the Sitecore platform. Why? I hear you ask… Well this I have found particularly useful when you want something in between an end user being able to ‘Read’ an item and not being able to. Consider a situation where instead of redirecting your end user to the login page, you can instead present ‘teaser’ content about why they should be joining up, and what they would gain by doing so. Furthermore, you could extend this to use conditional renderings to allow certain areas to hide or replace their data source giving a snapshot of what the page would look like if they sign up to your premium service. There are no doubt hundreds more use cases, but this particular one I have had requirement for more than once and it seems to be an elegant way to solve the problem.

Adding permissions
At its core, Sitecore relies on the web.config to provide the necessary permissions for items. This allows extending this permissions set to create your new permission(s) to be little more than adding an additional element in the appropriate section within the web.config or config include.

    <accessRights defaultProvider="config">
      <rights defaultType="Sitecore.Security.AccessControl.AccessRight, Sitecore.Kernel">
        <!--My custom access right-->
        <add name="item:newpermission" comment="New permission for items." title="New Permission"/>

By doing this, you should now find that ‘New Permission’ now appears as a right in the security editor.
Continue reading