Wednesday 5 June 2013

Redirect user to another SharePoint page after adding List Items

Redirect user to another SharePoint page after adding List Items

Redirecting a user to another SharePoint Application page immediately after a new item is added to a list isn’t exactly an easy task. You would think that by adding a new SharePoint event handler to a list, you could easily intercept the newly submitted item and perform additional actions. But, you would be mistaken.
For this example, a user needs to perform additional tasks after a new SharePoint list item has been added to a list. I my case, I wanted the user to create a new SharePoint 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.

What I had to do to redirect a user to another SharePoint application page
Create an Event Handler for the list and overriding the ‘ItemAdding’ method to:
  1. Create a new SharePoint list item
  2. Generate a unique ID for the new item. Since the ‘ItemAdding’ event happens before the item is added, no item ID exists.
  3. Redirect the user to a custom SharePoint 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.
  4. On submit, update the newly created list item (from step 1) with the recipients of the email.
  5. Using System.Net.Mail, send an email and redirect the user to a “successful submission” application page.

SharePoint Code to make this work:

01.using System;
02.using System.IO;
03.using System.Web;
04.using Microsoft.SharePoint;
05.using Microsoft.SharePoint.Utilities;
06.using Microsoft.SharePoint.WebControls;
07.using System.Web.UI.WebControls;
08.using System.Web.UI;
09.  
10.namespace SPD.MyCustomSolution {
11.class SPDEventHandlers : SPItemEventReceiver {
1.HttpContext context;
2.private string itemID = string.Empty;
3.  
4.public SPDEventHandlers() {
5.if (context == null) {
6.context = HttpContext.Current;
7.}
8.}
01.public override void ItemAdding(SPItemEventProperties properties) {
02.Guid entryGuid = Guid.NewGuid();
03.  
04.try {
05.using (SPSite site = new SPSite(properties.SiteId)) {
06.using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl)) {
07.SPList list = web.Lists[properties.ListId];
08.  
09.DisableEventFiring();
//BREAK THE ORIGINAL LIST ITEM THREAD AND START FROM SCRATCH
02.SPListItem itemToAdd = list.Items.Add();
03.  
04.//GET NEW FORM ENTRIES (aka 'AfterProperties') AND ADD THEM TO THE NEW LIST ITEM
05.try {
06.if (properties.AfterProperties["Title"] != null) {
07.itemToAdd["Title"] = properties.AfterProperties["Title"];
08.}
09.} catch (Exception ex) {
10.//LogEventError(ex.ToString());
11.}
12.  
13.//since new list item isn't know yet, create and add a unique id for this new entry
14.// the 'myEntryGUIDField' is a metadata field in the list.
15.try {
16.itemToAdd["myEntryGUIDField"] = entryGuid.ToString();
17.} catch (Exception ex) {
18.//LogEventError(ex.ToString());
19.}
20.  
21.//must update this list item to get the ID of the new item being created
22.itemToAdd.SystemUpdate();
 
01.//IF USER ADD's ATTACHMENT, ADD THEM TO THE NEW LIST ITEM
02.Stream fStream;
03.string fileName = string.Empty;
04.byte[] contents;
05.if (context != null) {
06.if (context.Request.Files.Count < 0) {
07.try {
08.  
09.HttpFileCollection aFiles = context.Request.Files;
10.for (int i = 0; i < aFiles.Count; i++) {
11.HttpPostedFile userFile = aFiles[i];
12.fileName = userFile.FileName.Substring(3);
13.  
14.fStream = userFile.InputStream;
15.contents = new byte[fStream.Length];
16.fStream.Position = 0;
17.  
18.fStream.Read(contents, 0, (int)fStream.Length);
19.fStream.Close();
20.  
21.itemToAdd.Attachments.Add(fileName, contents);
22.itemToAdd.SystemUpdate();
23.}
24.  
25.} catch (Exception ex) {
26.//LogEventError(ex.ToString());
27.}
28.} else {
29.// do something
30.}
31.}
01.EnableEventFiring();
02.  
03.//must cancel otherwise duplicate list entry will be made
04.properties.Status = SPEventReceiverStatus.CancelNoError;
05.  
06.}
07.}
08.} catch (Exception ex) {
09.//LogEventError(ex.ToString());
10.}
1.//AFTER NEW ITEM IS CREATED AND ATTACHMENTS ARE ADDED TO THE LIST, REDIRECT USER TO CUSTOM PAGE
2.SPUtility.Redirect(properties.RelativeWebUrl +
3."http://myownsite/Pages/CustomPage.aspx?myID=" +
4.entryGuid.ToString(), SPRedirectFlags.Trusted, context);
5.}
6.}
7.}

No comments:

Post a Comment