In this SharePoint tutorial, we will discuss how to show or display JavaScript alert in SharePoint 2013/2016 visual web part.
Here, I would like to enlighten your coding skills and enhance your application user-friendly messaging. Any Web user expects a success/error to be as an 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/2016.
In the SharePoint code-behind, we will not be able to get the javascript object to show an alert with an error message.
Display JavaScript Alert in SharePoint Visual 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 this:
try
{
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 outside the FARM.
You may like following SharePoint visual web part tutorials:
- Display tfs work items in GridView using SharePoint Visual Web Part
- Upload file to SharePoint 2016 document library with metadata programmatically using file upload control (Visual Web Part + Server Object Model)
- $Resources:core,Import ErrorMessage error while adding visual web part in SharePoint 2016
- How to implement expand/collapse in HTML table rows using jQuery
- How to include jQuery in SharePoint
This SharePoint tutorial, we learned how to show JavaScript alert in visual webpart in SharePoint 2013/2016.
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