Function

$().SPServices.SPRedirectWithID

Certification

Certified for SharePoint 2007 Not Tested with SharePoint 2010

Functionality

This function allows you to redirect to a another page from a new item form with the new item's ID. This allows chaining of forms from item creation onward.

Important Notes: This function will not work in anonymous mode. See this blog post for details on why. Also, there must be a redirect in place, as rdoyle78 points out in the comments below.

Tip: If you are using this function in conjunction with others in the library, call SPRedirectWithID first, as it will speed up the redirection.

How Does It Work?

Assuming your NewForm is called NewFormCustom.aspx and the redirectUrl is set to EditForm.aspx:

  • On the initial load of NewFormCustom.aspx, the form action is changed to point back to the same page, with ?Source=NewFormCust.aspx?ID=[the last ID created by the current user]%26RealSource=[the actual Source for the page]. The [the last ID created by the current user] is determined by calling the $().SPServices.SPGetLastItemId function.
  • When the form reloads, because the ID is present on the Query String, the jQuery function then waits until [the last ID created by the current user] is not equal to the value on the Query String. This ensures that the commit has completed. The [the last ID created by the current user] is again determined by calling the $().SPServices.SPGetLastItemId function.
  • The user should then be redirected to EditForm.aspx?ID=[the last ID created by the current user]

Syntax

$().SPServices.SPRedirectWithID({	
	redirectUrl: "",
	qsParamName: "ID"
});

redirectUrl
The page for the redirect. Upon save of the form, the page will refresh briefly and then be redirected to redirectUrl with the new item's ID on the Query String.

qsParamName
In some cases, you may want to pass the newly created item's ID with a different parameter name than ID. Specify that name here, if needed. The default is ID.

Example

By placing the code below into any NewForm.aspx (or your customized version of it), the user will be redirected to EditForm.aspx with the ID for the newly created item as the value for OrderID. Thus, if the code is placed into the page:
http://servername/sitepath/Lists/listname/NewForm.aspx
the user will be redirected to
http://servername/sitepath/Lists/listname/EditForm.aspx?OrderID=nnn
after creating the item.

<script language="javascript" type="text/javascript">
	$(document).ready(function() {
		$().SPServices.SPRedirectWithID({	
			redirectUrl: "EditForm.aspx",
			qsParamName: "OrderID"
		});
	});
</script>

The Source Query String parameter is preserved across the redirects, so:
http://servername/sitepath/Lists/listname/NewForm.aspx?Source=/sitepath/default.aspx
will redirect to:
http://servername/sitepath/Lists/listname/EditForm.aspx?OrderID=nnn&Source=/sitepath/default.aspx

It is possible to override the redirectUrl specified in the options by calling the page with a Query String parameter called RedirectURL. This allows for occasional overrides, if needed.
http://servername/sitepath/Lists/listname/NewForm.aspx?Source=/sitepath/default.aspx&RedirectURL=/sitepath/Lists/listname/EditForm2.aspx
will redirect to:
http://servername/sitepath/Lists/listname/EditForm2.aspx?OrderID=nnn&Source=/sitepath/default.aspx
regardless of the value specified for redirectUrl in the options.

Last edited Jan 28, 2011 at 3:17 PM by sympmarc, version 15

Comments

mstabile75 May 30, 2015 at 9:10 AM 
This is what I had to do to make it work in SP2010 (I was working with jquery.SPServices-2013.01.js)
1. Sharepoint continues to load scripts even after $(document).ready. One of these resets the form Action attribute. To get around this you need to add PreSaveAction function to your page so that you can set the form action attribute when you click save.
2. There was an error in the redirect string that had to be fixed
3. Have to read the IsDlg property when working with dialogs. I added in the parameters for that.

I hope this helps.

Here is the final code:

// This function allows you to redirect to a another page from a new item form with the new
// item's ID. This allows chaining of forms from item creation onward.
$.fn.SPServices.SPRedirectWithID = function (options) {

var opt = $.extend({}, {
redirectUrl: "", // Page for the redirect
qsParamName: "ID" // In some cases, you may want to pass the newly created item's ID with a different
// parameter name than ID. Specify that name here, if needed.
}, options);

SPServicesContext.thisList = $().SPServices.SPListNameFromUrl();
var queryStringVals = $().SPServices.SPGetQueryString();
var lastID = queryStringVals.ID;
var QSList = queryStringVals.List;
var QSRootFolder = queryStringVals.RootFolder;
var QSContentTypeId = queryStringVals.ContentTypeId;
var QSDlg = queryStringVals.IsDlg;

// On first load, change the form actions to redirect back to this page with the current lastID for this user and the
// original Source.
if(typeof queryStringVals.ID === "undefined") {
lastID = $().SPServices.SPGetLastItemId({
listName: SPServicesContext.thisList
});
$("form[name='aspnetForm']").each(function() {
// This page...
var thisUrl = (location.href.indexOf("?") > 0) ? location.href.substring(0, location.href.indexOf("?")) : location.href;
// ... plus the Source if it exists
var thisSource = (typeof queryStringVals.Source === "string") ?
"Source=" + queryStringVals.Source.replace(/\//g, "%2f").replace(/:/g, "%3a") : "";

var newQS = [];
if(typeof QSList !== "undefined") {
newQS.push("List=" + QSList);
}
if(typeof QSRootFolder !== "undefined") {
newQS.push("RootFolder=" + QSRootFolder);
}
if(typeof QSContentTypeId !== "undefined") {
newQS.push("ContentTypeId=" + QSContentTypeId);
}

var newAction = thisUrl +
((newQS.length > 0) ? ("?" + newQS.join("&") + "&") : "?") +
// Set the Source to point back to this page with the lastID this user has added
"Source=" + thisUrl +
"?ID=" + lastID + // Pass the original source as RealSource, if present
((thisSource.length > 0) ? ("%26RealSource=" + queryStringVals.Source) : "") +
// Pass the Dlg State if present
((typeof QSDlg !== "undefined") ? ("%26DlgStatus=" + QSDlg) : "") +
// Pass the override RedirectURL, if present
((typeof queryStringVals.RedirectURL === "string") ? ("%26RedirectURL=" + queryStringVals.RedirectURL) : "");
//Store the newAction variable somewhere you can access it later.
$('#onetIDListForm').append("<div id='actionNew' style='display: none'>"+ newAction +"<div>");
});
// If this is the load after the item is saved, wait until the new item has been saved (commits are asynchronous),
// then do the redirect to redirectUrl with the new lastID, passing along the original Source.
} else {
while(queryStringVals.ID === lastID) {
lastID = $().SPServices.SPGetLastItemId({
listName: SPServicesContext.thisList
});
}
// If there is a RedirectURL parameter on the Query String, then redirect there instead of the value
// specified in the options (opt.redirectUrl)
var thisRedirectUrl = (typeof queryStringVals.RedirectURL === "string") ? queryStringVals.RedirectURL : opt.redirectUrl;
var newLoc = thisRedirectUrl +
((thisRedirectUrl.indexOf("?") > 0) ? "&" : "?") + opt.qsParamName +
"=" + lastID +
((typeof queryStringVals.DlgStatus !== "undefined") ? "&IsDlg=" + queryStringVals.DlgStatus : "") +
((typeof queryStringVals.RealSource === "string") ? ("&Source=" + queryStringVals.RealSource) : "");
location.href = newLoc;
}
}; // End $.fn.SPServices.SPRedirectWithID


*************************** PreSaveForm function

function PreSaveAction() {
$("form[name='aspnetForm']").each(function() {
$(this).attr("action",$("#actionNew").text());
});
return true;
};

roastchicken Jul 30, 2014 at 12:49 PM 
Hi I've created a custom newform.aspx and added code. After saving the page is re-directed to the usual allitems.aspx not the edit page. Using sp2010. Has anyone solved this one ?

victorm Jul 15, 2014 at 6:25 PM 
Not working with SharePoint Server 2013

PhilMeacham May 29, 2013 at 10:19 PM 
Sorry bout this, I've searched and searched but I just can't find a straght forward guide on how to get this working.

I've created a file in a document library that basically has the jQuery library and the SPServices files defined and then the following code:
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$().SPServices.SPRedirectWithID({
redirectUrl: "Confirmation.aspx",
qsParamName: "ID"
});
});
</script>

Where the Confirmation.aspx is a page that I've tried with the ID in there and it works when you call it manually.

When I try to save it, nothing happens, it just saves the record and doesn't go to a confirmation page.

I'm not sure what is meant when it says that there must be a redirect in place. How do I do this? If there's anyone that can help it would be greatly appreciated.

I don't think I'm the only one with this problem. I'm using 2010 on premise.

Thanks in advance!!

Phil

stevensmith May 29, 2012 at 10:30 PM 
I posted a question to the discussion board, hope that someone can help me. http://spservices.codeplex.com/discussions/357185

benigemperle Mar 12, 2012 at 4:10 PM 
In SharePoint 2010 this function should be used in a function in the _spBodyOnLoadFunctionNames.push("MyLoadFunction"). Using $(document).ready doesn't work to me because SharePoint has always overwritten the form Action-URL change.

sympmarc Oct 18, 2011 at 2:02 PM 
jguzman277: Please post questions in the Discussions. M.

jguzman277 Oct 17, 2011 at 3:54 PM 
Any progress on SP2010? Would love to use this function

rdoyle78 Jan 26, 2011 at 8:12 PM 
Marc:

I just discovered something about SPRedirectWithID that I thought others might find useful.

I've got a list with a custom new item form, and it turns out that the SPRedirectWithID function will not work if there is not already a redirect statement applied to the 'save' button. The function works perfectly with a standard form, but it will fail if the "save" button only performs a commit - it must also have a redirect action applied. I dont' know if I just missed this in your setup, but it might be important for those of us who are using custom forms.

sympmarc Dec 16, 2010 at 6:07 PM 
mpeterson79: As indicated with the icons above, I haven't tested this with 2010. I have my doubts that it will work as written, frankly. I need to do a good round of testing for 2010 sometime very soon, probably for the v0.6.0 release. M.

mpeterson79 Dec 16, 2010 at 3:02 PM 
Any help getting this to work on SP2010 would be awesome! I've tried without success. Who knows, I may be doing it wrong..

sympmarc Nov 16, 2010 at 9:20 PM 
talonsblade: I'm not sure I understand your question. Can you post to the Discussions with more details? Thanks, M.

talonsblade Nov 16, 2010 at 6:14 PM 
is there a way to get a redirect before the new form? the list just keeps track of forms already filled out. my form is a php page. i was wondering if there was a way to redirect to my php page as soon as they click the new item on the sharepoint list not after they save the newform.aspx

dolan Oct 6, 2010 at 8:40 PM 
nevermind, you said : the last ID created by the current user, that answers my question. Thanks.

dolan Oct 6, 2010 at 8:36 PM 
This is exactly what I'm looking for. I need to chain an item creation to a form which collects some extra data that's used to pass to a workflow. this is a really cool way to do it. actually I can't figure any other way out, so you win. The only thing that worries me a little is whether or not there is an implicit race condition... what if two people hit submit right in a row, and before the page is reloaded, and I get the other guys ID?