Sue Hernandez's Blog

March 3, 2010

SharePoint 2007: Long Running Operations – timeout

Filed under: MOSS — Susan Hernandez @ 10:17 pm

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.

REFERENCES:

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;
longRunningJob.Begin();
// Some long processing here
Thread.Sleep(5000);
longRunningJob.End(url);


 

 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()
  {
    try
    {
      // Some Long Processing here
      foreach(SPUser user in web.Site.AllUsers)
      {
        // Do some operation
        this.StatusDescription = "Updating user " + user.Login;
        this.OperationsPerformed++;
        this.UpdateStatus();
      }
    }
    catch (Exception ex)
    {
      this.SetError(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;
  job.Start(SPContext.Current.Web);
  HttpContext.Current.Response.Redirect(job.StatusPageUrl);
}

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.

About these ads

6 Comments »

  1. Any ideas on how to accomplish something similar in WSS? This solution would work perfectly if I was using MOSS…

    Any help would be appreciated!

    Comment by Jeff Barton — May 20, 2010 @ 3:51 am

  2. So sorry I missed this comment for so long. And no, I’m not aware of any way to do this in WSS. If I find something I’ll post it here.

    Comment by Susan Hernandez — July 1, 2010 @ 5:04 pm

  3. Thanks so much for highlighting such great classes, you really save me a lot of time.

    Comment by Peter Riad — January 27, 2011 @ 8:20 am

  4. i also tried to user first scenario what u have said.. using splong, but after one hour that page went to page cannot be displayed mode. my httpresponsetimeour time is 3600 1 hour. so SPLongOperation does not worked as expected. my code will take atleast 2 hours.. what to do?? shall i try your second approach?

    Comment by guruprasadmarathe — February 15, 2013 @ 10:10 am

  5. I would definitely in your situation look in to inheriting from LongRunningOperationJob as in my second approach, yes. I don’t know even if that will handle a 2 hour job, but since it is a job in the background with just a status page that periodically refreshes, I don’t see why not. You don’t have to sit on the status page for the whole 2 hours do you? Just check on it periodically.

    Comment by Susan Hernandez — February 15, 2013 @ 10:11 pm

  6. I am using the same as given in the same code but when I am calling the updatestatus method , I am getting Site with 00000000 id is not found (file not found exception). Do I need to do something extra?

    Comment by Intekhab — October 23, 2014 @ 9:39 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Silver is the New Black Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: