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.


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";
        // Code that takes time to complete… 

Nice little piece that Microsoft added :)