September 10, 2008

How to display the SharePoint waiting page when doing long operations in your code

Displaying the SharePoint waiting page with the rotating gear image is easy. The Microsoft.SharePoint namespace provides the SPLongOperation class to play the trick.

As this class implements the IDisposable interface, the best is to use a using statement like in the following example:

void buttonProcess_Click(object sender, EventArgs e)
{
    using (SPLongOperation operation = new SPLongOperation(this.Page))
    {
        // Define the url to be redirected after the long operation
        string url = "http://myserver/myotherpage.aspx";
 
        operation.LeadingHTML = "Please wait...";
        operation.TrailingHTML = "Description...";
 
        operation.Begin();
        //Place here the code that takes a long time to be executed
        operation.End(url);
    }
}

MSDN link: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splongoperation.aspx

No comments: