Thursday, December 23, 2010

Display Success and Error messages in SharePoint 2010

One of the common functions which is required from any developer; is to display a Success and Error messages to the user, and it’s always better to use the same style of the main application.
In SharePoint: SPUtility class contains two methods to achieve this.
Success Message:

SPUtility.TransferToSuccessPage: It is used to display a Success Message, and it has two overloads.
SPUtility.TransferToSuccessPage Method (String message)

Example:
SPUtility.TransferToSuccessPage("Welcome, You are in Success Page");

Figure (1) shows the output of this code. In this case the “Ok” button will redirect to the current SharePoint Site home page.


Figure(1) Success Page

SPUtility.TransferToSuccessPage Method (String message, String nextUrl, String linkText, String linkUrl)

Example:
SPUtility.TransferToSuccessPage("Welcome, You are in Success Page", "/SitePages/Continue.aspx", "", "");
In this case the “Ok” button will redirect to “"/SitePages/Continue.aspx” page.

Error Message:
The same is applied to SPUtility.TransferToErrorPage: it is used to display error message.

SPUtility.TransferToErrorPage(String message)
Example:
SPUtility.TransferToErrorPage("Sorry it seems that you did a mistake. This is the error page"); 


Figure (3) Error Page

No comments:

Post a Comment