Add SharePoint UI Notification from C#

With the new UI model within SharePoint, there is a lot (LOTS) you can do from within JavaScript. 

As well as the client object model to get list & web details from SharePoint (from client side !) – you can also add a cool UI notification – like the ‘loading’ one shown here :


This uses the SP.UI.Notify.addNotification method – click the link to see some good examples at the MSDN site.

This is all great – but for SERVER side code – after a postback – from within C# – how do you do it ?!

Well, there is a way – in which you build up the JavaScript for the ‘addNotification’ – and then add the text/string to the Page output – and then it executes !  

The following C# method does the following :

  • Input parameter – of the current PAGE object – either inside a webpart, or ASCX control, for example
  • Input parameter – TEXT for the notification to display
  • Build up a JavaScript “SCRIPT” block – for the code to execute
  • IMPORTANT (this is the gotcha) – need to ‘delay until script loaded’ for the SP.JS file – otherwise, you get a NULL reference exception – ExecuteOrDelayUntilScriptLoaded
  • Add the JS to call to the actual method for the notification : SP.UI.Notify.addNotification
  • Create a new ASP.NET literal control – which adds the ‘raw text’ to the page
  • The notification is then shown, after the page refreshes – NICE !!


public static void AddSharePointNotification(Page page, string text)
    //build up javascript to inject at the tail end of the page
    StringBuilder stringBuilder = new StringBuilder();


    //First wait until the SP.js is loaded, otherwise the notification doesn’t work
    //gets an null reference exception
    stringBuilder.AppendLine("ExecuteOrDelayUntilScriptLoaded(ShowNotification, "sp.js");");

    stringBuilder.AppendLine("function ShowNotification()");
    stringBuilder.AppendLine(string.Format("SP.UI.Notify.addNotification("{0}");", text));


    //add to the page
    page.Controls.Add(new LiteralControl(stringBuilder.ToString()));



I’m sure this could be edited to do the SP.UI.Status.addStatus method also/instead.

Hope that’s helpful to you…!

** I’m planning to use this as a way of notifying the user of an EXCEPTION – within the end of a try-catch.

7 thoughts on “Add SharePoint UI Notification from C#

  1. Guilherme Morais August 3, 2011 / 4:57 pm

    Hi, this code is very useful, specially the ExecuteOrDelayUntilScriptLoaded trick.
    But this won´t work if you´re using the UpdatePanel, the literalcontrol is never written to the html.
    Regards from Brazil


  2. Jaybe January 24, 2012 / 10:08 am

    Note : if you are in userControl of a WebPart :

    “Parent.Controls.Add(” instead of “page.Controls.Add(“


    • Randy February 8, 2012 / 10:17 pm

      I am in a userControl of a webpart and when I put in Parent.Controls.Add, i am getting “An object reference is requred for the non-static field System.WebUI.Control.Parent.get. Can you post the rest of the code please.


  3. Randy February 9, 2012 / 7:57 pm

    Nevermind, i figured it out. I changed it to
    public void AddSharePointNotification(Page page, string text
    Thanks for the code anyway! Works great


  4. Diana March 29, 2012 / 9:16 am

    Very helpful thank you!


  5. Mario November 28, 2013 / 4:12 pm

    I have a problem…

    I found no way to get the Page object!
    In which way you get the current Page object?

    Apart from that… …very helpful! Thank you!


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s