Function
$().SPServices.SPRedirectWithID
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.
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: ""
});
redirectUrlThe 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.
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. Thus, if the code is placed into the page:
http://servername/sitepath/Lists/listname/NewForm.aspxthe user will be redirected to
http://servername/sitepath/Lists/listname/EditForm.aspx?ID=nnnafter creating the item.
<script language="javascript" type="text/javascript" src="../../jQuery%20Libraries/jquery-1.3.2.js"></script>
<script language="javascript" type="text/javascript" src="../../jQuery%20Libraries/jquery.SPServices-0.4.0.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$().SPServices.SPRedirectWithID({
redirectUrl: "EditForm.aspx"
});
});
</script>
The Source Query String parameter is preserved across the redirects, so:
http://servername/sitepath/Lists/listname/NewForm.aspx?Source=/sitepath/default.aspxwill redirect to:
http://servername/sitepath/Lists/listname/EditForm.aspx?ID=nnn&Source=/sitepath/default.aspxIt 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.aspxwill redirect to:
http://servername/sitepath/Lists/listname/EditForm2.aspx?ID=nnn&Source=/sitepath/default.aspxregardless of the value specified for redirectUrl in the options.