This project has moved and is read-only. For the latest updates, please go here.

$().SPServices.SPRedirectWithID Not Working

Jan 27, 2010 at 2:33 PM

I've put this call on a page but am not getting the expected behavior.  What happens is I can enter data on the form, but when I click OK I am taken back to the entry form again, with all the fields having the values I just entered.  If I click OK a second time, the entry is saved and I am taken to the displayItem form for the item I've just entered.  If I click Cancel instead, the item is not saved.

Below is the script code I am using:

<script language="javascript" type="text/javascript" src="http://myserver/sites/sitename/applications2/jQuery/jquery-1.3.2.js"></script>
<script language="javascript" type="text/javascript" src="http://myserver/sites/sitename/applications2/jQuery/jquery.SPServices-0.4.8.js"></script>
<script language="javascript" type="text/javascript"> 
$(document).ready(function() {
        redirectUrl: "DispForm.aspx"
Below are the URLs which appear at various stages 
Newitem page url before I put in jQuery

With jQuery:
First visit to the page:

After filling out form and clicking OK

Any ideas?



Jan 27, 2010 at 2:47 PM

There are a couple of people who are having issues with this function, and I haven't nailed it down yet.  Summarizing what I think I know:

  • This will not work if you are using FBA (nor will most of the Web Services calls)
  • Having a ContentTypeId on the Query String *may* cause a problem

Neither of these seem to apply in your case, however.  Since you are getting the ID back (6 in the example above), the SPGetLastItemId function is working correctly.  What happens when the user is returned to the NewForm is that the jQuery then waits until is "sees" a higher ID in the list for that same user.  Since the commit can take a little while longer than it takes the browser to refresh, that's the only "safe" approach.  I think that must be where the issue is for you.

So what does that do for you? Well, we could add a couple of debugging alerts or console messages into the jQuery to try to figure this out if you are game.


Jan 27, 2010 at 2:53 PM

I'd be willing to try some debugging if you have some suggestions.

Jan 27, 2010 at 4:08 PM

I had a nice long explanatory response almost ready for you, and my laptop decided to reboot to install some changes for me. Erg. Try the changes below and let me know what you see. If you want to send screenshots: marc dot anderson at



 // 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
  }, options);
  var thisList = listNameFromUrl();
  var vals = getQS();
  var lastID = vals["ID"];
  // 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(vals["ID"] == undefined) {
   lastID = $().SPServices.SPGetLastItemId({
    listName: 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 vals["Source"] == "string") ?
     "Source=" + vals["Source"].replace(/\//g, "%2f").replace(/:/g, "%3a") : "";
    var newAction = thisUrl + "?Source=" + thisUrl + "?ID=" + lastID +
     ((thisSource.length > 0) ? ("%26RealSource=" + vals["Source"]) : "") +
     ((typeof vals["RedirectURL"] == "string") ? ("%26RedirectURL=" + vals["RedirectURL"]) : "");
    $(this).attr("action", newAction);
  // 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(vals["ID"] == lastID) {
    lastID = $().SPServices.SPGetLastItemId({
     listName: 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 vals["RedirectURL"] == "string") ? vals["RedirectURL"] : opt.redirectUrl;
//   location.href = thisRedirectUrl + "?ID=" + lastID +
//    ((typeof vals["RealSource"] == "string") ? ("&Source=" + vals["RealSource"]) : "");
Mar 10, 2010 at 9:39 PM


I'm running into similar problems, although with slightly different results.  I've inserted the jQuery into a customized NewForm.aspx on a list called "Objectives". Here's my code:

<script language="javascript" type="text/javascript" src="https://myserver/sites/jQuery/jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript" src="https://myserver/sites/jQuery/jquery.SPServices-0.5.2.min.js"></script>
<script language="javascript" type="text/javascript">
	multiSelectColumn: "Collaboration Channel(s)"
	redirectUrl: "https://myserver/sites/Lists/Next%20Steps/NewForm.aspx"

NewForm.aspx url:


After filling out the form fields, and pressing okay, I get returned to the newitem page on the Objectives list:


I then correctly get redirected to the redirect url:


I fill out the form on the Next Steps list, and press OK, which gets this url and a blank Next Steps form:



I'm also not able to get the SPSetMultiSelectSizes to work at all on the NewForm.aspx on the Objectives list, unfortunately.


I'm hoping this feedback helps you out a bit in figuring out what's going on.  Let me know if I can get you any sort of debugging feedback as well!





Mar 11, 2010 at 9:13 PM


Sorry for the delayed response.  I'm wondering if the https is causing an issue.  When you're redirected to:
the source isn't right.  It shoulds be the original source page, which in this case is:

I'll take a look at it.

In the meantime, what's happening with SPSetMultiSelectSizes when you try to use it?  Is there any change in the form at all? Do you see any errors?


Mar 11, 2010 at 9:50 PM

I'm looking for what is different between my test environment and your environment.  Did you edit the default NewForm.aspx, probably by adding a CEWP? I don't know whay it would matter, but can you take a copy of NewForm.aspx and edit that instead?  That's how I go about it.  I end up with NewFormCustom.aspx and set that as the NewForm for the list

Looking at things, I don't think that https has anything to do with it.  When I get to this stage rather than what you see:


I see this:


I remember going back and forth with jj_the_skeptic on this in email.  Same thing for him. No idea why the %26 would decode in one environment and not another.


Mar 25, 2010 at 10:51 PM

Ok, good news (of sorts).  I can reproduce this in MOSS.  I'll be working on a fix.


Mar 28, 2010 at 5:43 AM

v0.5.4ALPHA1 posted which (hopefully) fixes the bug in SPRedirectWithID.

I'd appreciate any testing you could do.  Let me know whether it works for you.


Mar 29, 2010 at 3:31 PM
Edited Mar 29, 2010 at 3:31 PM


Unfortunately I had to leave this course of development with the project I'm working on due to tight timeframes.  But here's what I came up with, starting from


<script language="javascript" type="text/javascript" src="https://myserver/sites/jQuery/jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript" src="https://myserver/sites/jQuery/jquery.SPServices-0.5.4ALPHA1.js"></script>
<script language="javascript" type="text/javascript">
	$(document).ready(function() {
			redirectUrl: "https://myserver/sites/Lists/Next%20Steps/NewForm.aspx"

I get an error message:

An unexpected error has occurred.

Web Parts Maintenance Page: If you have permission, you can use this page to temporarily close Web Parts or remove personal settings. For more information, contact your site administrator.

Troubleshoot issues with Windows SharePoint Services.

Here's the URL that is displayed: 



Mar 29, 2010 at 4:16 PM

Thanks for testing, ennius.  As you can see in the resyulting URL, I'm not handling non-existing List and ContentTypeId Query String parameters gracefully.  I'll post a new alpha in a bit that is smarter.


Mar 30, 2010 at 5:26 AM

Just posted a stronger fix if you'd like to try it.  Thanks!


Mar 30, 2010 at 8:26 PM
Edited Mar 30, 2010 at 8:47 PM

Looks like you've got a winner, Marc.  It took me to the redirect location and passed the ID in the QueryString. :)


Hope the testing helps!

As an additional comment, I think a variation on this, where the ID is passed through the query string from the display form to a redirect location, would also be tremendously helpful.  Just a thought.

Mar 30, 2010 at 9:01 PM
Edited Mar 30, 2010 at 9:01 PM

Great! That's what I was hoping to hear!

Can you explain the second thought a little more? Do you mean DispForm.aspx?


Mar 30, 2010 at 9:05 PM

Exactly what I meant...the dispform.aspx.  While there are some cases where I do need the ID created on List 1 to be sent immediately to List 2, there are occasions where a user might want to just create an item in List 1 at that moment.  Later, when viewing the item created in List 1 through the dispform.aspx, they might want to create an associated item in List 2 with as many fields automatically filled in as possible based on the ID passed through the query string.  Hopefully that makes a bit more sense!

Mar 30, 2010 at 9:08 PM

That's probably more easily done by simply grabbing the ID from the Query String and doing a document.location redirect.  Not sure that a function in SPServices would add any real value, unless I'm missing something.  The value add for SPRediectWithID is that the ID isn't known until the item is created; with DispForm.aspx you know the ID up front.