Home > Development, SharePoint > Using SPLongOperation

Using SPLongOperation

The idea

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

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.

 /// <summary>
/// This test function will also call <see cref=”ALongRunningAction”/>, but it will
/// use <see cref=”SPLongOperation”/> to show a progress indication
/// </summary>
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
    catch (ThreadAbortException)
        // 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.

The result

I created a small WebPart with two buttons for this demonstration.

  1. Button one doesn’t use the SPLongOperation and you’ll see the page loading for 5 seconds.
  2. Button two uses the SPLongOperation and it displays the “Operation in Progress” progress page

Figure 1: Test WebPart

Figure 2: Operation in Progress screen

Download the code

Categories: Development, SharePoint
  1. Behzad
    January 8, 2010 at 22:13

    I have a problem in moss 2007 webpart in IE7 and IE8!
    downloaded and deployed the WEBPART example in


    the page only goes to the rotating gear page after an error of type ThreadAbortException is caught on the End Method :
    “Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.”

    which is weird cos the page should be refreshed into the gear page after the Begin!

    • Peter Heibrink
      January 10, 2010 at 15:08

      Hi Behzad,

      The webpart in the example has a sleep of 5 seconds. You might experience an time out on that step. Can you change that line to for example a sleep of 1 second?

      Using the SPLongOperation shows you the rotating image, but it doesn’t increase the time out of your web site. For that you can increase the time out settings of IIS in combination of the time out in the web.config (using the SPWebConfigModification class).


  2. Adrien M
    April 15, 2010 at 09:48

    Thanks for this post, very usefull! Gratz!

  3. August 4, 2011 at 12:59

    Thank you.. good tutorial..

  4. August 11, 2011 at 21:10

    Nice post, but SPLongOperation.End doesn’t call Response.Redirect, it uses Response.End directly and after that ThreadAbortException is thrown. And I believe It’s not connected with other threads. I have mentioned about this fact in my blog post here – http://dotnetfollower.com/wordpress/2011/08/sharepoint-how-to-use-splongoperation/.

  5. Gandalf1970
    June 19, 2012 at 16:18

    I would like to use SPLongOperation in an event receiver when I update an item of a list. Everything is already in place except the message that I need to show users. Unfortunately, I don’t know how to refer to this.Page. How can I do that? Thanks!

  6. Peter Heibrink
    June 19, 2012 at 17:41

    The constructor needs the page object and I don’t believe that is accessable from an event receiver. What you could do however, is redirect in the event receiver to a custom application page. Then in that page call the SPLongOperation, perform your action and redirect to another page when the action is done.

  7. January 30, 2013 at 19:06

    Hi! Someone in my Facebook group shared this site with us so I came
    to give it a look. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers!
    Exceptional blog and excellent style and design.

  8. February 2, 2013 at 08:34

    Hi there this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code
    with HTML. I’m starting a blog soon but have no coding expertise so I wanted to get advice from someone with experience. Any help would be enormously appreciated!

  9. sex
    February 12, 2013 at 07:35

    Hi there! I could have sworn I’ve been to your blog before but after browsing through a few of the posts I realized it’s new to me.
    Regardless, I’m definitely pleased I found it and I’ll be book-marking it and checking back

  10. March 6, 2013 at 02:32

    Howdy! This post couldn’t be written any better! Reading this post reminds me of my good old room mate! He always kept chatting about this. I will forward this page to him. Pretty sure he will have a good read. Thank you for sharing!

  11. May 14, 2013 at 09:14

    My brother suggested I might like this web site. He was once totally right.
    This put up truly made my day. You can not imagine simply how much time I had spent for this info!

    Thank you! buy raspberry ketone – buy raspberry ketone – raspberry ketone side effects

  12. January 26, 2014 at 14:14

    Wonderful beat ! I would like to apprentice while you
    amend your web site, how can i subscribe for a blog site?
    The account aided me a acceptable deal. I had been tiny bit acquainted
    of this your broadcast offered bright clear idea

  13. May 13, 2014 at 17:17

    Glad I found this on google.

  14. June 4, 2014 at 09:21

    I for all time emailed this blog post page to all my
    friends, as if like to read it after that my contacts will

  15. June 20, 2014 at 13:27

    Great post. I was checking continuously this blog and I am impressed!
    Extremely helpful information particularly the last part 🙂 I care for such information a lot.
    I was seeking this certain info for a very long time. Thank you and good

  16. July 3, 2014 at 17:54

    I’ve been surfing online more than 4 hours today, yet I never found any interesting article like yours.
    It is pretty worth enough for me. In my opinion,
    if all webmasters and bloggers made good content as
    you did, the net will be a lot more useful than
    ever before.

  1. July 19, 2010 at 17:04
  2. January 13, 2012 at 09:55
  3. March 19, 2012 at 17:17
  4. July 19, 2012 at 02:53
  5. June 9, 2015 at 09:56

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Ben Prins

What I want to remember about SharePoint


my world of work and user experiences

Bram de Jager - Coder, Speaker, Author

Office 365, SharePoint and Azure

%d bloggers like this: