Tuesday, September 4, 2012

Redirect from NewForm.aspx to DispForm.aspx or EditForm.aspx of newly created item


Redirect from NewForm.aspx to DispForm.aspx or EditForm.aspx of newly created item.

You may need to redirect from NewForm.aspx to DispForm.aspx (or other page) of newly created item when creating SharePointsolutions like Invoices and Orders management. There are a couple of blogs out there that already have a solution for this. Because at the moment of item creation there is no item ID yet all we can do is to redirect to other page where we can query SharePoint list using web services for the last created item by current user. Then we can just redirect to wanted page using queried item ID.

Thanks to SharePoint Drive team.

http://www.sharepointdrive.com/blog/redirect-from-newform-aspx-to-dispform-aspx-or-editform-aspx-of-newly-created-item/


HTTP Context:
http://www.entwicklungsgedanken.de/2008/03/27/redirecting-from-newformaspx-to-dispformaspx-after-creating-a-new-item/



USE THE BELOW CODE IN ITEM ADDING EVENT
Using
SPSite site = new SPSite(properties.SiteId))          
{
using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
 {
SPList list = web.Lists[properties.ListId];
DisableEventFiring();
SPListItem itemToAdd = properties.ListItem;
foreach (SPField field in itemToAdd.Fields)
{
if (!field.Hidden && !field.ReadOnlyField && field != null && field.InternalName != "Attachments")
{
if (properties.AfterProperties[field.InternalName] != "")
{
 itemToAdd[field.InternalName] = properties.AfterProperties[field.InternalName].ToString();                
           }
}
}
 itemToAdd.Update();
 EnableEventFiring();

// Redirect
SPUtility.Redirect("DispForm.aspx?ID=" + itemToAdd.ID, SPRedirectFlags.DoNotEndResponse, httpCurrentCtx);

 }






No comments:

Post a Comment