While performing very long operation in SharePoint 2013 / 2010 or earlier versions, a request time out error will be thrown if it takes more time to complete. Now we can see how to avoid this error by using SPLongOperation class available in SharePoint
The below code snippet shows you how to create a long running process in SharePoint. You can also provide custom information to the end user by assigning HTML snippets to LeadingHTML and TrailingHTML properties of SPLongOperation
The Code
using (SPLongOperation longProcess = new SPLongOperation(this.Page))
{
try
{
longProcess.LeadingHTML = "Please wait while Processing the Data";
longProcess.TrailingHTML = "Started processing data ...";
longProcess.Begin();
//Start your Long Operation Here
.
.
.
.
.
//Long Operation Ends Here…
longProcess.End(Request.Url.AbsolutePath, SPRedirectFlags.DoNotEndResponse, this.Context, "s=1");
}
catch
{
longProcess.End(Request.Url.AbsolutePath, SPRedirectFlags.DoNotEndResponse, this.Context, "s=0");
}
}
Processing the Output
Once the Operation is completed the page will be redirected to the URL specified in End method of SpLongOperation. In this sample, I am appending a query string “s” with value 1 if it is success and with value 0 if the operation failed. If it is success or failure I am redirecting it to the same page, so, in the page load I can decide what message has to be conveyed to the user depending on the availability and value of query string.
Leave a comment