Archive

Archive for April, 2010

System Alert in sitecore

27/04/2010 1 comment

When working with large Sitecore implementation and customers that have a lot of editors maintaining the content of their website, often makes updating the system becomes a hurdle. Because editors may or may not work in on the same floor or even the same building, so even if you have an contact person who’s responsibility is to notify editor about the downtime, it is possible that one or more editors haven’t heart about the downtime and haven’t save the what they where doing the second the system is taken offline, a valuable work may have been lost.
So I thought what would be more smart the implementing a System alert/ notifier system that alert the users about the forthcoming downtime. This Alert could as in this example be a simple javascript alert.
My solution is build around simple Javascript calling a webservice which looks for alert placed inside sitecore and display them as a simple javascript alert. Yes some may argue that I have some hardcoded path string and what have we not, but it is left to you to move these to fx. The web.config. Even more this solution I maybe a little over the edge when looking at the implementation, but I se so many usages for this so I went ALL-IN as implemented with interface and using a provider model. The solution is build and tested against a Sitecore 6.2, but nothing wrong with using on other Sitecore versions.
But here goes first the javascript since Sitecore editors have three different editors Page Edit, Content Editor and the Desktop. So we need to include the javascript in three different files, and because of the we need to ensure that the file is only loaded once so logged into the Desktop and opening the content editor doesn’t give two warnings, hence the cookie check. Now to javascript, it’s all simple stuff.
The javascript should be include in these three files
Webedit:
sitecore\shell\Applications\WebEdit\WebEditRibbon.aspx
Content Editor:
sitecore\shell\Applications\Content Manager\Default.aspx
Desktop:
sitecore\shell\Applications\Startbar\Startbar.xml


/* Function for calling the webservice                             */
function callWebservice() {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET", "/Components/SystemNotifier/AjaxWebservice/SystemNotifier.asmx/GetAlerts", false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send();

if (xmlhttp.status == 200) {
var str = xmlhttp.responseXml.getElementsByTagName("string").item(0).text;
return str;
}
}

/* function that Get system alerts */
/* update timer by calling the webservice    */
function GetSystemAlerts() {
var alertString = callWebservice();
if (alertString != "") {
alert(alertString);
//increase time to next call so we dont get same alert twice
setTimeout("GetSystemAlerts()", 125000);
}
else {
setTimeout("GetSystemAlerts()", 60000);
}
}

var cookieName = "SitecoreSystemNotifier";

function writeCookie() {
document.cookie = cookieName;
}

function cookieExists()
{

if (document.cookie.length >0 )
{
var offset = document.cookie.indexOf(cookieName);
if (offset != -1)
return true;
return false;
}
return false;
}

function init(){
if(!cookieExists()){
writeCookie();
//SetTimeout in ms
setTimeout("GetSystemAlerts()", 60000);
}
}

init();

Okay now that we have the javascript we need the webservice to be called. It’s fairly simple when using the Provider.


namespace SystemNotifier.AjaxWebservice
{
///
/// Summary description for SystemNotifier
///
[WebService(Namespace = "http://pentia.dk/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class SystemNotifier : WebService
{

[WebMethod]
public string GetAlerts()
{
ISystemAlert alert = AlertProvider.NextAlert;
if (alert != null)
return alert.Text;
return "";
}

private SystemAlertProvider _provider;
private SystemAlertProvider AlertProvider
{
get
{
if (_provider == null)
_provider = new SystemAlertProvider();
return _provider;
}
}

}
}

And now to the provider implementation.


public class SystemAlertProvider
{
private IEnumerable _alerts;
public IEnumerable Alerts
{
get {
if(_alerts == null)
_alerts = GetAlertsFromSitecore();
return _alerts;
}
}

private TimeSpan timespan = new TimeSpan(0, 1, 0);
private IEnumerable GetAlertsFromSitecore()
{
ChildList childList = AlertRootItem.Children;
foreach(Item child in childList)
{
ISystemAlert alertItem = new SystemAlert(child);
if(alertItem.AlertTime > DateTime.Now.Subtract(timespan))
yield return alertItem;
}
}

private const string sitecoreRootPath = "/sitecore/system/SystemAlertNotifier";
private Item _rootItem;
private Item AlertRootItem
{
get
{
if(_rootItem == null)
_rootItem = Database.GetItem(sitecoreRootPath);
return _rootItem;
}
}

private const string _databaseName = "master";
private Database Database
{
get
{
return Database.GetDatabase(_databaseName);
}
}
public ISystemAlert NextAlert
{
get
{
if(Alerts.Count() > 0)
return Alerts.OrderBy(w => w.AlertTime).First();
return null;
}
}
}

And finally the Alert interface and implementation of the same.

Inteface


public interface ISystemAlert
{
DateTime AlertTime { get; }
String Text { get; }
}

Implementaion


public class SystemAlert : ISystemAlert
{
public SystemAlert(Item item)
{
Item = item;
}

private Item Item
{
get;
set;
}

private const string _alertTimeField = "SystemAlert_AlertTime";
public DateTime AlertTime
{
get
{

DateField dateField = Item.Fields[_alertTimeField];
return dateField.DateTime;
}
}

private const string _textField = "SystemAlert_Text";
public string Text
{
get { return Item[_textField]; }
}
}

Now we got all the code working so now we need to have someway to get the info, let’s use a sitecore item. So here is a snapshot of the how my sitecore item looks.

So this is pretty much everything you need to have a system alert system up and running inside sitecore. Remember to edit hardcode root path to system alert root folder.
You can download the project in the download section link here.

And hope you can see the posiblities in this solution or implementaion, you could scheduled downtown and have email alert, downtime calendar and much much more hope you enjoy,

Categories: C#, Javascript, Sitecore 6 Tags: