$().SPServices.SPRedirectWithID Not Working

Jan 27, 2010 at 1: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() {
    $().SPServices.SPRedirectWithID({	
        redirectUrl: "DispForm.aspx"
    });
});
</script>
Below are the URLs which appear at various stages 
Newitem page url before I put in jQuery
http://myserver/sites/sitename/applications2/Lists/Applications/NewForm.aspx?RootFolder=%2Fsites%2Fsitename%2Fapplications2%2FLists%2FApplications&Source=http%3A%2F%2Fmyserver%2Fsites%2Fsitename%2Fapplications2%2FLists%2FApplications%2FAllItems%2Easpx

With jQuery:
First visit to the page:
http://myserver/sites/sitename/applications2/Lists/Applications/NewForm.aspx?RootFolder=%2Fsites%2Fsitename%2Fapplications2%2FLists%2FApplications&Source=http%3A%2F%2Fmyserver%2Fsites%2Fsitename%2Fapplications2%2FLists%2FApplications%2FAllItems%2Easpx

After filling out form and clicking OK
http://myserver/sites/sitename/applications2/Lists/Applications/NewForm.aspx?Source=http://myserver/sites/sitename/applications2/Lists/Applications/NewForm.aspx?ID=6%26RealSource=http://myserver/sites/sitename/applications2/Lists/Applications/AllItems.aspx

Any ideas?

Thanks

 

Coordinator
Jan 27, 2010 at 1: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.

M.

Jan 27, 2010 at 1:53 PM

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

Coordinator
Jan 27, 2010 at 3: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 sympraxisconsulting.com

M.

 

 // 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);
alert(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
    });
alert(lastID);
   }
   // 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 8:39 PM

Marc-

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">
$().SPServices.SPSetMultiSelectSizes({
	multiSelectColumn: "Collaboration Channel(s)"
}); 
$().SPServices.SPRedirectWithID({	
	redirectUrl: "https://myserver/sites/Lists/Next%20Steps/NewForm.aspx"
});
</script>

NewForm.aspx url:

https://myserver/sites/Lists/Objectives/NewForm.aspx?RootFolder=%2Fsites%2FLists%2FObjectives&Source=https%3A%2F%2Fmyserver%2Esites%2FLists%2FObjectives%2FAllItems%2Easpx

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

https://myserver/sites/Lists/Objectives/NewForm.aspx?Source=https://myserver/sites/Lists/Objectives/NewForm.aspx?ID=3%26RealSource=https://myserver/sites/Lists/Objectives/AllItems.aspx

I then correctly get redirected to the redirect url:

https://myserver/sites/Lists/Next%20Steps/NewForm.aspx?ID=4&Source=https://myserver/sites/Lists/Objectives/NewForm.aspx?ID

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

https://myserver/sites/Lists/Next%20Steps/NewForm.aspx?ID=4

 

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!

 

Cheers!

 

 

Coordinator
Mar 11, 2010 at 8:13 PM

ennius:

Sorry for the delayed response.  I'm wondering if the https is causing an issue.  When you're redirected to:
https://myserver/sites/Lists/Next%20Steps/NewForm.aspx?ID=4&Source=https://myserver/sites/Lists/Objectives/NewForm.aspx?ID
the source isn't right.  It shoulds be the original source page, which in this case is:
https://myserver/sites/Lists/Objectives/AllItems.aspx

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?

M.

Coordinator
Mar 11, 2010 at 8: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:

https://myserver/sites/Lists/Objectives/NewForm.aspx?Source=https://myserver/sites/Lists/Objectives/NewForm.aspx?ID=3%26RealSource=https://myserver/sites/Lists/Objectives/AllItems.aspx\

I see this:

https://myserver/sites/Lists/Objectives/NewForm.aspx?Source=https://myserver/sites/Lists/Objectives/NewForm.aspx?ID=3&RealSource=https://myserver/sites/Lists/Objectives/AllItems.aspx\

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.

M.

Coordinator
Mar 25, 2010 at 9:51 PM

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

M.

Coordinator
Mar 28, 2010 at 4: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.

Thanks,
M.

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

Marc-

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

https://myserver/sites/Lists/Mission/NewForm.aspx

<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() {
		$().SPServices.SPRedirectWithID({	
			redirectUrl: "https://myserver/sites/Lists/Next%20Steps/NewForm.aspx"
		});
	});
</script>

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: 

https://myserver/sites/Lists/Mission/NewForm.aspx?List=undefined&RootFolder=/sites/Lists/Mission&ContentTypeId=undefined&Source=https://myserver/sites/Lists/Mission/NewForm.aspx?ID=0%26RealSource=https://myserver/sites/Lists/Mission/AllItems.aspx

 

Coordinator
Mar 29, 2010 at 3: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.

M.

Coordinator
Mar 30, 2010 at 4:26 AM

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

M.

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

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

https://myserver/sites/lists/Next%20Steps/NewForm.aspx?ID=3&Source=https://myserver/sites/Lists/Mission/AllItems.aspx

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.

Coordinator
Mar 30, 2010 at 8:01 PM
Edited Mar 30, 2010 at 8: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?

M.

Mar 30, 2010 at 8: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!

Coordinator
Mar 30, 2010 at 8: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.

M.