Inheriting and Extending Sitecore JavaScript

Whenever possible I try to extend Sitecore as cleanly as possible, such as trying to patch into pipelines and event handlers or overriding dialog without replacing the default ones. It’s not always possible and sometimes you just have to dive it and get things done. You’ll see the same thing in my blog posts, some are more “cleanly” implemented than others.

There have been a number of times when I need to supplement some existing code or to fix a bug in some Sitecore code. The usual way I have done this is to get dirty and edit the JS files directly. Not great, but it was quick 😀 It’s also the exact same thing that Sitecore Support does whenever we have to apply a JS fix.

I’ve generally found that it’s easier to extend C# code, since it’s where I am more familiar. But usually with the JS I have hacked and updated the default Sitecore files. Cos you know, when you simply need to add an extra 9 characters, you can’t be spending the whole day trying to figure out “a more clean way”. Ain’t nobody got time….

But that was then and this is now, so I’ll share some techniques for extending Sitecore JavaScript without resorting to direct file edits. It also makes it difficult to build something into a shareable and installable module. All of these methods require you to inject in some additional resource files that I have previously blogged about.

Read More

Advertisements

Environment Styler for Sitecore

tl;dr; Install the module, set the config value to match your environment, have a stylised login screen and header bar per environment.

Have you ever sat there working on some task and then suddenly someone asks you to take a look at an issue on the Production environment? So you log onto that server, resolve the issue, get distracted for a few minutes by cat videos and then get back to what you were doing. But you suddenly realise that those changes you were just making was not on your local environment, you still had the Production site open in your browser tab! Oh noes!

Oh noes!

The problem is that all the environments all looks exactly the same… the only difference being that teeny tiny URL bar, the URL in which probably also looks very similar apart from some environment prefix.

Read More

Updating Sitecore Telerik RTE to disable XHTML and strip MS-Word Formatting on Paste

The RTE field in Sitecore is a little bit old fashioned and a little stricter than the most of us would like, especially when you are working with tech savvy editors who may prefer to work directly in the HTML. The current project I am working on is based on the Foundation framework and like most frameworks a lot of the features are enabled using data-* attributes.

Unfortunately the Telerik controls used in Sitecore try to be super helpful and strip out any invalid XHTML. Also unfortunately data-* attributes are HTML5, and not XHTML 😦

Read More

SVG in Media Library Polluting Log Files with Errors

Storing SVGs in the Sitecore Media Library is pretty simple, just add a new mediatype in config. This has been well documented and there are several posts as well as Stackoverflow/forum answers for this. e.g. https://community.sitecore.net/developers/f/8/t/1670

And this works fine and everything renders great, but whilst trying to sort out some servers issues and trawling through the Sitecore logs I noticed it was littered with Errors like so:

5920 15:34:49 ERROR Could not run the 'getMediaStream' pipeline for '/sitecore/media library/github-logo-01'. Original media data will be used.
Exception: System.ArgumentException
Message: Parameter is not valid.
Source: System.Drawing
   at System.Drawing.Bitmap..ctor(Stream stream)
   at Sitecore.Resources.Media.ImageEffectsResize.ResizeImageStream(Stream inputStream, TransformationOptions options, ImageFormat outputFormat)
   at Sitecore.Resources.Media.ImageThumbnailGenerator.GetStream(MediaData mediaData, TransformationOptions options)
   at Sitecore.Resources.Media.MediaData.GetThumbnailStream(TransformationOptions options)
   at Sitecore.Resources.Media.ThumbnailProcessor.Process(GetMediaStreamPipelineArgs args)
   at (Object , Object[] )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Resources.Media.Media.GetStreamFromPipeline(MediaOptions options, Boolean& canBeCached)

Read More

User Specific or Multi Site Specific CSS styles in Sitecore Rich Text Editor

Following a question on Stackoverflow about filtering the css classes shown in the Rich Text Editor control, I thought I would share some simple code which is useful in various scenarios.

The RTE control used in Sitecore is Telerik RadEditor. The implementation is very flexible, and a lot of the configuration can be changed by changing the profile in the core database under /sitecore/system/Settings/Html Editor Profiles. You can easily switch the profile used on a per field basis by setting the source on the field definition:

RTE-Field-Source

Read More

Custom Sitecore field for storing Key/Value Data with Lookups

On a recent question about storing custom field attributes for a field in Sitecore on Stackoverflow just over a week ago I suggested looking at the Name Value List and Name Lookup List field types. I have to admit that I have never used this field before, but knew of their existence and the general premise of the field.

Name Value List field

If you are unaware of this field, it allows you to store Key/Value pairs of data. The underlying raw value of the field is then stored as url type parameters:

key1=value1&key2=value2&key3=value3

Read More

jQuery Modal Dialogs for Sitecore 6.5-7.0 to fix Chrome 37+ browser issues

tl;dr A fix for Google Chrome 37+ which removed support for showModalDialog() and broke a lot of functionality in older releases of Sitecore and can be found on Github

11/09/2014: Sitecore have officially released patches for v6.4 to 7.0 for this bug. Glad to see that I was not a million miles off in my implementation though. Please use these patches instead: https://kb.sitecore.net/articles/581527

With the release of Google Chrome 37 we have finally lost our beloved modal dialogs created with showModalDialog(). This is used in all versions of Sitecore up to version 7.1. There is no official workaround for this yet, aside from use EnableDeprecatedWebPlatformFeatures Chrome policy, use a different browser or upgrade as stated in this knowledge base.

There was a fair amount of talk of this issue towards the end of last week, I also contributed on the main SDN thread and it seems we totally missed the issue being raised by Alex Pershteyn a few weeks ago. Well it appears those showModalDialog() is deprecated messages in the console window actually meant something and apparently ignoring them didn’t make the problem go away 😦

showModalDialog deprecated

Read More

Hiding Content Editor Fields Depending on Selected Values

tl;dr Custom Field which hides other fields in the Content Editor depending on the selected value

A recent question on Stackoverflow asked if it was possible to hide fields in the Sitecore Content Editor when certain values are selected. This is a pretty common (and easy task) on any normal web site and it makes sense from a usability point of view to only show those fields which are relevant to the user. I gave a half answer to the question that a Composite Custom Field could be created to fire off the relevant Javascript and hide the sibling fields on change. I was unsure however how to hide the fields on document load.

Read More

Adding custom Javascript and Stylesheets in the Content Editor

tl;dr Add custom resources in the Sitecore Content Editor from config setting

I recently created some custom fields in which I needed to add some custom JavaScript. I looked at a couple of other implementations of custom fields but they all worked on the individual control, not at an Item level or adding snippets of Javascript by inlining it adding if from code-behind declarations. I also wanted to avoid modifying any standard Sitecore files if at all possible, which some of the other custom field implementation had done and makes installing multiple packages tricky since each would overwrite the changes of the others. Aside from which, I wanted to avoid it from an upgrade point of view. I also needed to update the UI when the content had been loaded so would need to listen for that event.

Read More