How to redirect a user to a custom page after adding a new List Item
November 16th, 2011
This took me a while to figure out how to do this so I thought I’d share my experience.
Problem:
User needs to perform additional tasks after a new list item has been added to a list. I my case, I wanted the user to create a new list item of information that needed to be emailed to a certain group of people, based on what he/she selected in the new list item form.
Solution:
Create an Event Handler for the list and overriding the ‘ItemAdding’ method to:
- Create their new item
- Generate a unique ID for the new item. Since the ‘ItemAdding’ event happens before the item is added, no item ID exists.
- Redirect the user to a custom application page, allowing the user to select the recipients of a custom email about the newly created list item. In my case, I wanted the user to select the recipients from another list. This new custom application page queries that list for recipients.
- On submit, update the newly created list item (from step 1) with the recipients of the email.
- Using System.Net.Mail, send an email and redirect the user to a “successful submission” application page.
Code:
using System;
using System.IO;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace SPD.MyCustomSolution {
class SPDEventHandlers : SPItemEventReceiver {
HttpContext context;
private string itemID = string.Empty;
public SPDEventHandlers() {
if (context == null) {
context = HttpContext.Current;
}
}
public override void ItemAdding(SPItemEventProperties properties) {
Guid entryGuid = Guid.NewGuid();
try {
using (SPSite site = new SPSite(properties.SiteId)) {
using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl)) {
SPList list = web.Lists[properties.ListId];
DisableEventFiring();
//BREAK THE ORIGINAL LIST ITEM THREAD AND START FROM SCRATCH
SPListItem itemToAdd = list.Items.Add();
//GET NEW FORM ENTRIES (aka 'AfterProperties') AND ADD THEM TO THE NEW LIST ITEM
try {
if (properties.AfterProperties["Title"] != null) {
itemToAdd["Title"] = properties.AfterProperties["Title"];
}
} catch (Exception ex) {
//LogEventError(ex.ToString());
}
//since new list item isn't know yet, create and add a unique id for this new entry
// the 'myEntryGUIDField' is a metadata field in the list.
try {
itemToAdd["myEntryGUIDField"] = entryGuid.ToString();
} catch (Exception ex) {
//LogEventError(ex.ToString());
}
//must update this list item to get the ID of the new item being created
itemToAdd.SystemUpdate();
//IF USER ADD's ATTACHMENT, ADD THEM TO THE NEW LIST ITEM
Stream fStream;
string fileName = string.Empty;
byte[] contents;
if (context != null) {
if (context.Request.Files.Count > 0) {
try {
HttpFileCollection aFiles = context.Request.Files;
for (int i = 0; i < aFiles.Count; i++) {
HttpPostedFile userFile = aFiles[i];
fileName = userFile.FileName.Substring(3);
fStream = userFile.InputStream;
contents = new byte[fStream.Length];
fStream.Position = 0;
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
itemToAdd.Attachments.Add(fileName, contents);
itemToAdd.SystemUpdate();
}
} catch (Exception ex) {
//LogEventError(ex.ToString());
}
} else {
// do something
}
}
EnableEventFiring();
//must cancel otherwise duplicate list entry will be made
properties.Status = SPEventReceiverStatus.CancelNoError;
}
}
} catch (Exception ex) {
//LogEventError(ex.ToString());
}
//AFTER NEW ITEM IS CREATED AND ATTACHMENTS ARE ADDED TO THE LIST, REDIRECT USER TO CUSTOM PAGE SPUtility.Redirect(properties.RelativeWebUrl + "http://myownsite/Pages/CustomPage.aspx?myID=" + entryGuid.ToString(), SPRedirectFlags.Trusted, context); } } }
Hope this helps.

Pavel
Very interesting, thanks!
Can’t find anything about myEntryGUIDField in the documentation. Is that how Microsoft named it?