Sitecore Contextual Desktop Wallpaper

So, we have had a few instances of end users switching the Sitecore context database and editing content in the wrong place.

This has caused some confusion, and in rare occasions, loss of data.

We pondered if it would be possible to highlight which database you are in, by manipulating the wallpaper displayed on the user desktop.

And this is what I came up with, the “Contextual Wallpaper” handler

Very simple to implement, a few lines of code, a configuration entries and a tweak to the Sitecore user profile template.

The Code:

Create a new Generic Handler in your project and call it ContextualImage.ashx

Implement both System.Web.IHttpHandler and System.Web.SessionState.IRequiresSessionState interfaces

using System.Drawing;
using System.Web;
 
namespace Arktouros.CustomSitecore
{
    public class ContextualImage : System.Web.IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

Read the file from the file system.

//read the image path from the configuration
            var bmp = Bitmap.FromFile(HttpRuntime.AppDomainAppPath + System.Configuration.ConfigurationManager.AppSettings["LogoImageLocation"]);

Append a blank area to the image, and set it to be the same colour as the logged in users’ background

//add in extra 80px to fit text in
var newImage = new Bitmap(bmp.Width, bmp.Height + 80);
var gr = Graphics.FromImage(newImage);
gr.DrawImageUnscaled(bmp, 0, 0);
 
//set default background color
string htmlColorCode = "#FFFFFF";
//add this field to the user profile template if not already there
if (!string.IsNullOrEmpty(Sitecore.Context.User.Profile.GetCustomProperty("WallpaperBackground")))
{
    htmlColorCode = Sitecore.Context.User.Profile.GetCustomProperty("WallpaperBackground");
 
}
//setup rectangles to add lines to the image
Rectangle rec1 = new Rectangle(0, bmp.Height, bmp.Width, 80);
Rectangle rec2 = new Rectangle(0, bmp.Height + 20, bmp.Width, 20);
Rectangle rec3 = new Rectangle(0, bmp.Height + 40, bmp.Width, 20);
Rectangle rec4 = new Rectangle(0, bmp.Height + 60, bmp.Width, 20);
 
//set background colour so it looks transparent
System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml(htmlColorCode);
SolidBrush bgColor = new SolidBrush(col);
gr.FillRectangle(bgColor, rec1);
gr.FillRectangle(bgColor, rec2);
gr.FillRectangle(bgColor, rec3);
gr.FillRectangle(bgColor, rec4);

Now we need to set the colour to be used for writing the text, this is generated as an opposite of the user background

//get new brush with opposite colour to the background colour
System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(Color.FromArgb(255 - col.R, 255 - col.G, 255 - col.B));

Now write our text back to the rectangles that we setup earlier

//sort out fonts to use
Font Fnt = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
Font bldFnt = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
 
//write into line 1
gr.DrawString("You are logged in as:", Fnt, brush, rec1);
//write into line 2
gr.DrawString(Sitecore.Context.User.Name, bldFnt, brush, rec2);
 
if (!string.IsNullOrEmpty(Sitecore.Configuration.State.ContentDatabase.ToString()))
{
    //write into line 3
    gr.DrawString("Viewing data from:", Fnt, brush, rec3);
    //write into line 4
    gr.DrawString(Sitecore.Configuration.State.ContentDatabase.ToString().ToUpper() + " database", bldFnt, brush, rec4);
 
}

You will notice above that I used Sitecore.Configuration.State.ContentDatabase instead of Sitecore.Context.Database. This is because the ContentDatabase is the DB that gets switched, not the Context database.

After that it is simply a case of returning the image in the Response object

//return the image
HttpContext.Current.Response.ContentType = "image/gif";
newImage.Save(HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

Build your project and place your dll in the bin folder and handler under the “shell” site path (/sitecore/shell/). This is required so that it picks up the correct context / content databases when it is viewed from the Sitecore desktop.

The Configuration

Add this to the appSettings region of your web.config, this is the root relative path to the physical image file.

<add key="LogoImageLocation" value ="/logo.jpg"/>

The Sitecore Changes

Login to the sitecore desktop and switch to the CORE database.

Add a new field to the user profile template (default item ID: {642C9A7E-EE31-4979-86F0-39F338C10AFB}):

field name: “WallpaperBackground” and leave the Type dropdown on the empty item, as below, save the template.

sitecore

Then edit the User profile item ( default item ID: {AE4C4969-5B7E-4B4E-9042-B2D8701CE214} path: /sitecore/system/Settings/Security/Profiles/User) to set the wallpaper as /sitecore/shell/ContextualImage.ashx (path/filename of your handler) and the WallpaperBackground to the HTML colour value of your choice (#FFFFFF)

Save the profile item, then switch back to the MASTER database and you should see you image with the Username of the logged in user and the ContentDatabase you are editing.

Done.

time for a beer….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s