In this post I’m going to show how implemented indicator of long operations in Sharepoint internally. Most of posts about SPLongOperation are limited by examples of using it. I will go further and show how it is implemented internally. So lets start.
If you worked with Sharepoint then you will probably saw OTB indicator of long operations:
This indicator also can be used in your custom code, e.g. in handler of button OnClick event or any other place. In order to show it you need to use SPLongOperation class (defined in Microsoft.SharePoint.dll). The usage is quite simple: all you need is to create instance of SPLongOperation, call Begin method at the beginning of operation and End method on the end. Also you can specify your own title and description for long operation.
For example I created simple application _layouts page:
There is a button on the page. When user clicks this button the following indicator of long operation will be shown:
After 5 seconds indicator is hided and page is shown again. Note that operation will be executed synchronously so if it will take more time than request timeout specified in your web application you will get Request timeout exception. And you will understand why it happens when see the rest of this article.
Most of posts which tell about SPLongOperation are finished in this place. Lets go further and see under the hood. What happens when you call SPLongOperation.Begin() method? Lets see this method using reflector:
It reads content of gear.aspx page which is located in 12/template/layouts folder on file system. Then it reads content line by line and replaces placeholders by real values. At first it replaces System.Threading.Thread.CurrentThread.CurrentUICulture.LCID placeholder by real integer value of current locale. So css path from
if you use English locale (lcid = 1033). Then it replaces resources placeholders, so strings like:
will be expanded to:
I.e. code retrieves multipages_direction_dir_value resource object from wss.resx file and replaces it in output html.
If you will see inside gear.aspx file you will find several other placeholders in layout:
Placeholders SPLongOperation.BeginContent, SPLongOperation.EndContent, SPLongOperation.Dots are replaced by empty string. Placeholders <!-- LEADING HTML –> and <!-- TRAILING HTML –> are replaced by LeadingHTML and TrailingHTML properties of SPLongOperation object.
Now very important moment: in the Begin() method SPLongOperation writes response until SPLongOperation.Dots placeholder (see code above). So html will NOT contain the following lines:
And this function is assigned to onload event of body element. It means that until another function with non-empty body will be written to the response, first loaded function with empty body will be used (actually this 1st function is not called at all because body element is not fully loaded). And as you probably already guess SPLongOperation.End(…) method replaces SPLongOperation.RedirectUrl placeholder by redirect URL (in example above this is the same page as was initially requested) and writes the rest of file to the response:
is replaced by
(I assumed that URL of testing page is test.aspx). Unfortunately SPLongOperation.End(…) method is obfuscated but I found one single post here which shows implementation of this method:
That how SPLongOperation is implemented internally. Hope it will help you to understand Sharepoint mechanisms more deeply.