Sue Hernandez's SharePoint Blog

SharePoint and Related Stuff

Monthly Archives: March 2010

SharePoint 2007: Long Running Operations – timeout

I have recently been challenged with a web part in v3 (MOSS) that has to iterate through a bunch of users’ permissions in v2 (WSS 2.0), which takes forever and always seems to time out.  I found 2 items that I would like to share with you, one of which takes care of processes that run a long time but don’t time out, and one of which that handles an operation so long it will time out.


The first method, which doesn’t solve timeout issues, but does provide feedback for long operations, is via the SPLongOperation class.

Gears Page in SharePoint

Gears Page in SharePoint

 Basically, what you do is instantiate an SPLongOperation object, set its LeadingHTML and TrailingHTML properties, and then wrap it arount a long running process.  You would use in a web part as follows:

In the class, declare an SPLongOperation object:

SPLongOperation longRunningJob;

In the CreateChildControls method, instantiate the object to a new instance and set its parameters:

longRunningJob = new SPLongOperation(this.Page);
longRunningJob.LeadingHTML = "This is a bold description of my operation";
longRunningJob.TrailingHTML = "This is a normal or sub-description of my operation";

Where-ever you have your long running code, “wrap” the SPLongOperation object around it:

// Some URL to redirect to once the processing has completed
string url = SPContext.Current.Web.Url;
// Some long processing here


 The second method is to inherit from the LongRunningOperationJob which can be found in the Microsoft.SharePoint.Publishing dll which you can find in your Program Files (C:\Program Files\Microsoft Office Servers\12.0\Bin for example).  You override the DoWork method to actually complete your long running task.  Take for example the following instance of a class inheriting from LongRunningOperationJob:

public class UpdateUsersPermissionsJob : LongRunningOperationJob
  private SPWeb web;
  private string currentlyLoggedInUser;
  UpdateUsersPermissionsJob(SPWeb web, string currentlyLoggedInUser)
    _web = web;
    _currentlyLoggedInUser = currentlyLoggedInUser;
  public override void DoWork()
      // Some Long Processing here
      foreach(SPUser user in web.Site.AllUsers)
        // Do some operation
        this.StatusDescription = "Updating user " + user.Login;
    catch (Exception ex)

You would call this operation from inside your code where you want to actually perform the action:

private void cmdSubmit_Click(object sender, EventArgs e)
  SPWeb web = SPContext.Current.Web;
  string user = SPContext.Current.Web.CurrentUser.Login;
  UpdateUsersPermissionsJob job = new UpdateUsersPermissionsJob(web, user);
  job.Title = "Updating users for web " + web.Title;
  job.WaitMessage = "Please wait while the users are updated...";
  job.RedirectWhenFinished = false;
  job.UserCanCancel = false;
  job.TotalOperationsToBePerformed = web.Site.AllUsers.Count;
  job.GearsImageUrl = "~/_layouts/images/gears_an.gif";
  job.MillisecondsToWaitForFinish = 2500;

This brings you to a page that will show a progress bar and the message you set as the WaitMessage.

In Progress page

In Progress bar for LongRunningOperationJob Status Page

 Check out the reference links for more code and more examples.