Show JavaScript alert in SharePoint Webpart

In this article, I would like to enlighten your coding skills and enhance your application user friendly messaging. Any Web user expect a success / error to be as alert message, but the  challenge here is that how we show the alert in a catch block of code behind in SharePoint visual webpart / classic webpart. Here we will see how we can display JavaScript alert box inside a catch block in SharePoint visual web part using C#.Net in SharePoint 2013. If you are new to visual web part, Download FREE PDF on Visual web part in SharePoint 2016.

Issue :: Can’t get JavaScript alert object in WebPart code behind

In SharePoint code behind we will not be able to get the javascript object to show an alert with error message.

SharePoint deveopment training course

Solution (Display JavaScript Alert in SharePoint Web Part):

use the below single line to show the error message in C# code behind in SharePoint.

ScriptManager.RegisterStartupScript(this, GetType(), “flash”, “alert(‘Error Message’);”, true);

The complete code will look like as this:

SPSite objSite = new SPSite(“http://sharepoint13:1245”); // I am trying to get the object of a site collection out side the FARM
catch(Exception ex)
ScriptManager.RegisterStartupScript(this, GetType(), “flash”, “alert(‘ You can not get the object out side this FARM’);”, true);

Note: In the above code block I am trying to get an object of a site collection out side the FARM.

Hope this helps, happy development in SharePoint !!!

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)


About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →