Skip Ribbon Commands
Skip to main content

Robin | zevenseas | SharePoint Blog

:

The zevenseas Community > Blogs > Robin | zevenseas | SharePoint Blog > Posts > SP.UI.Notify is awesome
March 23
SP.UI.Notify is awesome

When I saw the notifications in 2010 for the first time, I really thought that could be used for some very cool stuff. For example, wouldn’t it be cool to set up a message somewhere centrally like “Servers are going down this weekend for maintenance” and everybody sees that notification in their browser.

Well, I managed to get such a nice little app working (you wouldn’t have thought that eh? ;) So what did was the following:

  • Create a control that displays the message based on the centrally managed setting
  • Create an application page where you can set the message and other properties.
  • Make use of the AdditionalPageHead delegate to load the control on every page

Below are some screenies how it looks:

notify 

notifyteamsite

I had some problems to display the notification on the load of the page, since it was working on postbacks. There was something going on with the order of loading of the several javascripts. While debugging the page using the IE Developer Toolbar it seemed that only two classes of the SP.UI namespace were loaded (and thus not the Notify one). So I had to figure a way on how to make my control display the notification after the SP.js was loaded. Luckily my buddy Waldek had came across such a problem before and told me to make use of the following JavaScript function “ExecuteOrDelayUntilScriptLoaded(‘functionToExecuteAfterScriptIsLoaded’, ‘javascriptFileToLoad’)”, and that the trick! :)

Here is the source of the UserControl where I add javascript to the page that loads the Notification:

protected override void CreateChildControls()
{
    SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
    Notification notification = webApplication.GetChild<Notification>("Notify");
    if (notification.Display)
    {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.AppendLine("<script>");
        //First wait until the SP.js is loaded, otherwise the Notification doesn't work and we get an null reference exception
        stringBuilder.AppendLine("ExecuteOrDelayUntilScriptLoaded(Initialize, \"sp.js\");");
        stringBuilder.AppendLine("function Initialize() {");
        stringBuilder.AppendLine("SP.UI.Notify.addNotification(\"" + notification.Message + "\", " + notification.Sticky.ToString().ToLower() + ", \"Tip\", null);");
        stringBuilder.AppendLine("}");
        stringBuilder.AppendLine("</script>");
        this.Controls.Add(new LiteralControl(stringBuilder.ToString()));
    }
    base.CreateChildControls();
}

Pretty neat eh? I’m not really a big fan of adding a script like I just did but at least it get’s the job done… the complete solution is on CodePlex, so if you’re interested to take a look at it be my guest!

For the next release/update I want to work on :

  • Asynchronous updates
  • Make use of the status bar as well so you can choose to use the Status Bar of the Notifications
  • Make a ribbon action and add that to the WebApplication Ribbon and use a popup for the Settings.aspx

 

Posts that helped me out (thanks guys!) :

 

@Zimmergren : this was the cliff hanger I was tweeting about ;)

Comments

Tobias Zimmergren

Roger that mate ;-)
Good post, like the dynamic addition of notifications like you've done.

Z
System Account on 24/03/2010 01:18

Robin

@Tobias, thanks buddy! :)
System Account on 24/03/2010 05:33

panoone

This looks like just what I need. Will it work with 2007 or is this only for early adopters of 2010?
System Account on 11/04/2010 15:42

Robin

Hi panoone,

this is only available for SP2010 indeed..

Robin
System Account on 11/04/2010 23:46

vishal

Kewl! Thanks..
System Account on 26/08/2010 22:40

Notification missing assembly

I get an error at this line:

Notification notification = webApplication.GetChild<Notification>("Notify");

Notification is missing a reference.
Could please tell me which one is it?

One more question: I would like to write some notification or messages in the status bar but from code behind based on specific events. How can I accomplish that?

Thank you in advance.
 on 25/11/2010 03:13
 

 Statistics

 
Views: 10464
Comments: 5
Tags:
Published:1709 Days Ago