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.
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?
Here comes the trick…
Grab the objects in the constructor, store them and use later when you need them.
1: private SPContext _spContext;
2: private HttpContext _httpContext;
4: public EventReceiver()
6: _spContext = SPContext.Current;
7: _httpContext = HttpContext.Current;
10: public override void ItemAdded(SPItemEventProperties properties)
15: if (_spContext == null)
17: // item has been created via code from timerjob. no context and no need to redirect
20: string url = SPUrlUtility.CombineUrl(_spContext.Web.ServerRelativeUrl, "_layouts...") + properties.ListItemId;
23: catch (ThreadAbortException e)
25: // occures if redirected
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.
1: using (var longRunning = new SPLongOperation(Page))
5: // do your long running operation here
7: longRunning.End(null, SPRedirectFlags.Default, Context, null,
8: "window.frameElement.commonModalDialogClose(1, null);");
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.
1: SPList list = ...
3: // attach EventReceiver
4: var receiverDefinition = list.EventReceivers.Add();
5: receiverDefinition.Type = SPEventReceiverType.ItemAdded;
6: receiverDefinition.Assembly = "yourAssemblyFullName";
7: receiverDefinition.Class = "Your EventReceiver class (incl. namespace)";
Basically that’s it. A combination of known tasks to create a new solution.
You can show a work-in-progress dialog to a user, when a new SPListItem is created. Here are the steps:
- Use a synchronous asynchronous event
- Redirect to a Layouts-Page
- Use the SPLongOperation class to do your work
- Have fun and happy customers