Sometimes the actions triggered by users can take a while to process. This can for example happen when you create a site collection. At that time you:
Don’t want your users to continue clicking on the button, the action should finish first. With the example of the creation of a site collection, you don’t want two attempts of creating a site collection with the same URL.
Users must be made aware that the action might take a while and have the idea something is really happening behind the scenes.
Microsoft provided some nice ways to have implement those long running operations. One is SPLongOperation which shows the user the “Operation in Progress” screen and it is actually pretty easy to implement.
The implementation of the SPLongOperation is pretty easy. In a nutshell you create a new SPLongOperation, provide the different texts you want to display and you start your operation.
The next code example will be triggered by a button in a WebPart. It doesn’t actually do anything, but just let the current thread sleep for 5 seconds. In that time the “Operation in Progress” page is being displayed.
/// This test function will also call <see cref=”ALongRunningAction”/>, but it will
/// use <see cref=”SPLongOperation”/> to show a progress indication
private void ALongActionWithProgressIndicator()
// Determine the page to navigate to when the operation is successful
string successUrl = SPContext.Current.Web.Url;
// Create a new SPLongOperation
SPLongOperation longOperation = new SPLongOperation(this.Page);
// Provide the text displayed in bold
longOperation.LeadingHTML = “Long running operation is being performed”;
// Provide the normal formatted text
longOperation.TrailingHTML = “Please wait while your request is being performed. This can take a couple of seconds.”;
// Let’s start the code that takes a while from here
// The code that might take a while
//When the action is performed, the page will be redirect to this url
// Don’t do anything, this error can occur because the SPLongOperation.End
// performs a Response.Redirect internally and doesnt take into account
// that other code might still be executed
catch (Exception ex)
// When an exception occurs, the page is redirected to the error page.
// Redirection to another (custom) page is also possible
There is one thing you might want to take into account when you use the SPLongOperation. The SPLongOperation.End internally uses a Response.Redirect which doesn’t take into account that you might have other code that is executed at the same time in a different thread. In order to prevent a ThreadAbortException message to display, you might want to catch that error.
I created a small WebPart with two buttons for this demonstration.
Button one doesn’t use the SPLongOperation and you’ll see the page loading for 5 seconds.
Button two uses the SPLongOperation and it displays the “Operation in Progress” progress page
Figure 1: Test WebPart
Figure 2: Operation in Progress screen