Sunday, March 29, 2009

Show “Operation In Progress” spinning wheel in custom code

While doing custom coding in SharePoint if you have any long running operations(like creation of site) which take some significant time to complete then you always want to give a hint to the end user to be patient and that the operation is in progress and it is not in the hung state.

SharePoint does it smartly by showing the smart spinner.

spin_wheel

While coding custom stuff I always wanted to show the same spinner as that would be consistent with the out of box SharePoint behavior.

Initially I used to(and saw many others too) doing it by embedding some JavaScript to load the image, until I stumbled upon this.

It was exciting to see that this is there as a built in function - "SPLongOperation".
Below is the way we can use this in code :

using (SPLongOperation longOperation = new SPLongOperation(this.Page)) 
{ 
        longOperation.LeadingHTML = "Put Leading HTML here";
        longOperation.TrailingHTML = "Put Trailing HTML here";
        longOperation.Begin(); 
        // Code that takes time to complete… 
        longOperation.End(strURL);
}


Nice little piece that Microsoft added :)

5 comments:

Anirudh May 21, 2009 at 12:26 AM  

nice to see you blog your learnings!!keep it up bro!!

Saugat March 11, 2010 at 12:11 AM  

Hi,
I just saw your blog url in one of your questions you posted in MSDN regarding
"Open in new window does not work when SSRS report is hosted in SharePoint".

I too am having the same error. Could you resolve it? If yes, can you please let me know how.

dotNetFollower August 15, 2011 at 9:05 AM  

Hello!
I have a similar article in my blog. Additionally I explained the reason of ThreadAbortException, which is sometimes thrown. The article is here - http://dotnetfollower.com/wordpress/2011/08/sharepoint-how-to-use-splongoperation/
Thanks!

Connolly Parr April 20, 2012 at 10:34 PM  

This is quite simple development work out once you comprehend how the LongOperation class is adjusting the browse. This has saved lot of time as I was not knowing that this could be done in such a simple way.

MeenuDevan November 19, 2012 at 12:38 AM  

Hi..nice tips. Thanks