Do long running operations on SPListItem creation

Do long running operations on SPListItem creation

Events on SPListItems like ItemAdding or ItemAdded are nothing new. Many of you have already used them. Recently I had a requirement to create a new SPSite, when an item in a list has been created. So an ItemReceiver was my choice.

But the customer wants something special 🙂 During the creation process, which takes some seconds, the user should see a loading animation.

image

Here comes the problem. The ItemEventReceiver is running in the background, and has no knowledge about the GUI process. Well, at least if it is running asynchronous. A very good explanation can be found here: Using synchronous “after” events (e.g. ItemUpdated) in SharePoint 2010.

The short summary: If you use synchronous events, they get executed in the same thread and you have the HttpContext and SPContext!

You already tried this and didn’t have the context objects?

image

image

Here comes the trick…

image

Grab the objects in the constructor, store them and use later when you need them.

Ok. We’ve successfully redirected to another page in our creation page, which can be a modal dialog or full frame page. I’ve not been able to use the Page to start a SPLongOperation with. A NullReference Exception has been thrown. So my solution was another Layouts-Page, which then starts the SPLongOperation. When it is done, the Layouts-Page is closed. By closing it, the modal dialog from the Item Creation process also vanishes.

I’ve chosen the ItemAdded and not ItemAdding event, because ItemAdding did not like the redirect. The item did not get created.

The Layouts-Page uses SPLongOperation e.g. in CreateChildControls.

After the operation has been executed, the SPLongOperation is ended and a script passed as last parameter in the End() method is executed. You don’t need script tags here.

To register the ItemEventReceiver to execute synchronously, use the following code.

Basically that’s it. A combination of known tasks to create a new solution.

Summary

You can show a work-in-progress dialog to a user, when a new SPListItem is created. Here are the steps:

  1. Use a synchronous asynchronous event 🙂
  2. Redirect to a Layouts-Page
  3. Use the SPLongOperation class to do your work
  4. Have fun and happy customers
1 Comment

Yanis

Reply

Hi René;

You could please send me your complet code : I Have try to show a busy/progress indicator while running lengthy operation in an event receiver ItemAdded (create a new site). I wanted to use the SPLongOperation class, but it requires a PAGE object in its constructor. Not available in an eventreceiver ?

Error is on the constructor : SPLongOperation longOperation = new SPLongOperation(this.Page);

And when I use your solution I have every time the context equal to NULL when I execute my code :

public class CreateSite : SPItemEventReceiver
{
private SPContext _spContext;
private HttpContext _httpContext;
public CreateSite()
{
_spContext = SPContext.Current;
_httpContext = HttpContext.Current;
}

public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
SPWeb web = properties.OpenWeb();
if (_spContext == null)
{
return;
}
string urltest = SPUrlUtility.CombineUrl(_spContext.Web.ServerRelativeUrl, “_layouts…”) + properties.ListItemId;
_httpContext.Response.Redirect(urltest);

if (web.Exists)
{

using (SPLongOperation op = new SPLongOperation(Page))
{
op.LeadingHTML = “…”;
op.TrailingHTML = string.Format(“XXXXX” + namesubsite);
op.Begin();

… long process

op.End(web.Url);

}

}}

Regards;

 

Leave a Reply