Redirect to Another Page from NewForm.aspx with the New Item’s ID: Redux
UPDATE 2009-10-19: I solved this for real in the jQuery Library for SharePoint Web Services function called $().SPServices.SPRedirectWithID. Feel free to read this post, but this is the solution you’re looking for.
The most visited post on my blog, hands down, is the one on Redirect to Another Page from NewForm.aspx with the New Item’s ID. (Oddly enough, the one about Harvey Balls for Office Documents is right up there, too.)
I never liked the solution that much, as it seems sort of messy, but it works. Over the last few days, I’ve been trying to figure out a way to take advantage of the SharePoint Web Services which we’ve “wrapped” in the jQuery Library for SharePoint Web Services to come up with a cleaner approach. Unfortunately, I’m still stumped.
The basic issue is this: until you commit the item which you are creating, there is no ID for it. There’s nothing in NewForm.aspx to hook into to grab that new ID because it doesn’t exist yet. This makes chaining pages together in a wizard-like way sort of kludgy, at least from NewForm.aspx to the next page in the chain.
Right now, I have a jQuery function for the library called $().SPServices.SPGetLastItemId in the lab which returns the latest ID for a given user on a given list. This is a bit cleaner than the DVWP approach, as you can just drop a call into your interstitial page like this:
var thisID = $().SPServices.SPGetLastItemId({ webURL: "/Demos", listName: "Sales Opportunities" });
The function uses two Web Services calls to get the latest item’s ID. First, it calls the GetUserInfo operation of the Users and Groups Web Service to find out the user’s ID based on the user’s account (either specified in the options or as returned by the $().SPServices.SPGetCurrentUser() function). Next it calls the GetListItems operation of the Lists Web Service to find the latest item created for that user.
But you still need to have that interstitial page, which is what I don’t like in the first place! Before I make this function a part of the library, I want to think some more about how we can improve upon this method.
If you have any ideas on how to get this to work better, drop me a line. I’d love to offer up a better solution!
For us dummies out there that are trying to do stuff way above our heads, could you give us an example of your interstitial page? I assume we use the same code for the submit button in your previous blog, so the interstitial page will be named GetLastID.aspx. Am I correct?
Hi Marc,
I might have a solution for you on this problem.
Here are the steps to accomplish this task:
1. Create a new list (if not already)
2. On NewForm.aspx type toolpaneview=2 in the address bar to add new webparts
3. Click “Add webpart” and choose “Content Editor Webpart”
4. Paste following into Source Editor:
5. Exit edit mode and try saving new form
Example:
http://www.sharejpoint.com/examples/Lists/SaveAndEdit/AllItems.aspx
Cheers!
Samir:
Unfortunately, code doesn’t make it into comments on WordPress. I’d really like to see what you tried to post, though. If you can send it to me using the Contact form above, I’ll insert it into your comment and take a look.
Thanks!
M.
Hi Marc,
I have posted example and instructions here:
1. http://www.sharejpoint.com/examples/
2. Login with demouser as explained on the site
3. Click on the SaveAndEdit link under examples
Hye Marc,
I’m not sure where I did wrong. But I can’t make this work.
1. uploaded the j-1.3.2.min.js and SPservice-0.4.0.js
2. Put the code in my CEWP
3. Exit edit mode
4. Try add new item. It redirect me to AllItems page.
How can I change this.
Note: I change form EditForm.aspx to DispForm.aspx… it does’nt matter. does it?
Thank you for your time..
ashiena:
First off, can you update to v0.4.6, which is the current release? I made some significant changes to SPRedirectWithID in v0.4.5, so let’s at least be sure we’re on the same version. It sounds like you have gone through the right steps. If you still have an issue, please post to the Codeplex Discussions area.
M.
thank you for your time and effort … it works ! :)
Mark,
All I want to do is point to a different page after an element is created passing the same param I used to get into the child add page.
want to redirect to /LIST/BP/DispForm_CUstom.aspx?ID={ParamBPID}
rather than
allitems.aspx
How do I do this
Matt:
Take a look at the SPRedirectWithID function in SPServices.
M.
Marc:
We are trying to do this with SharePoint 2010.
However, I can’t do this with an additional library.
Is there a way after opening the Modal Dialog, with a NewForm.aspx, to get the actual ID of the item?
Even the get latest by this user involves risk.
Ken:
If there were, we wouldn’t need to go through this monkey business. Items are created asynchronously, as near as I can tell, and I’ve found no other way to get at the id, short of creating each item with your own script or code.
M.
Hi Marc,
I don’t know what I’m doing wrong, but it’s not working for me on SP2013. I keep getting redirected to the Allitems.aspx page. Here’s what I did.
1 – Created a custom NewForm for my list.
2 – Added the following to the NewForm. (Btw. I tried it befor without the PreSaveAction and when it didn’t work, I added the PresaveAction() )
//$(document).ready(function() {
//alert(“jQuery”);
//alert($().SPServices.SPGetCurrentSite());
//});
function PreSaveAction() {
$().SPServices.SPRedirectWithID({
redirectUrl: “EditForm.aspx”,
qsParamName: “ID”
});
return true;
}
$(document).ready(function() {
alert(“jQuery”);
alert($().SPServices.SPGetCurrentSite());
$().SPServices.SPRedirectWithID({
redirectUrl: “EditForm.aspx”,
qsParamName: “ID”
});
});