Add SharePoint UI Notification from C#

By | February 18, 2011

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 :

image

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();

    stringBuilder.AppendLine("<script>");

    //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("{");
    stringBuilder.AppendLine(string.Format("SP.UI.Notify.addNotification("{0}");", text));
    stringBuilder.AppendLine("}");

    stringBuilder.AppendLine("</script>");

    //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

    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

    Reply
  2. Pingback: Add SharePoint notification message after postback « discoveringsharepoint

  3. Jaybe

    Note : if you are in userControl of a WebPart :

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

    Reply
    1. Randy

      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.

      Reply
  4. Randy

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

    Reply
  5. Mario

    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!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *