This is another method for redirecting a user to a custom page on “OK” and another page on “Cancel”.
I have described two methods in this previous article. This method is a god replacement for the “simple redirect” described in the previous article.
As always we start like this:
Create a document library to hold your scripts (or a folder on the root created in SharePoint Designer). In this example i have made a document library with a relative URL of “/test/English/Javascript” (a sub site named “test” with a sub site named “English” with a document library named “Javascript”):

Add a CEWP below your list-form in NewForm or EditForm, and add this code:
<script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
fields = init_fields();
// Where to go when cancel is clicked
goToWhenCanceled = '/test/English/YouCanceled.aspx';
// Edit the redirect on the cancel-button's
$('.ms-ButtonHeightWidth[id$="GoBack"]').each(function(){
$(this).click(function(){
STSNavigate(goToWhenCanceled);
})
});
// Edit the form-action attribute to add the source=yourCustomRedirectPage
function setOnSubmitRedir(redirURL){
var action = $("#aspnetForm").attr('action');
var end = action.indexOf('&');
if(action.indexOf('&')<0){
newAction = action + "?Source=" + redirURL;
}else{
newAction = action.substring(0,end) + "&Source=" + redirURL;
}
$("#aspnetForm").attr('action',newAction);
}
/*
// Use this for adding a "static" redirect when the user submits the form
$(document).ready(function(){
var goToWhenSubmitted = '/test/English/ThankYou.aspx';
setOnSubmitRedir(goToWhenSubmitted);
});
*/
// Use this function to add a dynamic URL for the OnSubmit-redirect. This function is automatically executed before save item.
function PreSaveAction(){
// Pass a dynamic redirect URL to the function by setting it here,
// for example based on certain selections made in the form fields like this:
var mySelectVal = $(fields['MySelect']).find(':radio:checked').next().text();
if(mySelectVal=='Option one'){
var dynamicRedirect = '/test/English/YouSelectedOptionOne.aspx';
}else if(mySelectVal=='Option two'){
var dynamicRedirect = '/test/English/YouSelectedOptionTwo.aspx';
}
// Call the function and set the redirect URL in the form-action attribute
setOnSubmitRedir(dynamicRedirect);
// This function must return true for the save item to happen
return true;
}
function init_fields(){
var res = {};
$("td.ms-formbody").each(function(){
if($(this).html().indexOf('FieldInternalName="')<0) return;
var start = $(this).html().indexOf('FieldInternalName="')+19;
var stopp = $(this).html().indexOf('FieldType="')-7;
var nm = $(this).html().substring(start,stopp);
res[nm] = this.parentNode;
});
return res;
}
</script>
This script is by default setup with a “dynamic” redirect for “OK” based on the selection made in a “Choice-field” of type “Radio Buttons” with the options “Option one” and “Option two”.
The “OK” redirect can be set to a “static” value if you comment out the function “PreSaveAction()” and uncomment the “$(document).ready(…” function.
The cancel-redirect is set in the top pf the script in the variable “goToWhenCanceled”. The script modifies the “click” attribute of both cancel-buttons and adds this redirect.
Ask if something is unclear
Regards
Alexander
November 21, 2009 at 6:58 pm |
[...] Redirect from NewForm to EditForm or custom page By Alexander 21.11.2009 A new article describing another method for redirecting a user to a custom page on “OK” and another page on “Cancel” is found here. [...]
November 23, 2009 at 1:22 am |
[...] Redirect on NewForm and EditForm another method [...]
November 25, 2009 at 7:51 pm |
hye,
I have download the jquery using SmartTools J Query loader.
I have change the script path to my folder ie: /js/jquery.js
I add the CEWP to my newForm.aspx of one of my list.
but I still can not get the result I wanted.
How do I know my .js is working or my modified script has an error?
thank you in advanced
November 25, 2009 at 8:39 pm
Hi,
Try to add this alert to your code:
It will confirm that the jQuery library is loaded by alerting this text:
function (E, F) { return new (o.fn.init)(E, F); }Alexander
December 8, 2009 at 1:45 am |
[...] This post was mentioned on Twitter by SharePoint, SharePoint. SharePoint said: Redirect on NewForm and EditForm another method SharePoint JavaScript http://bit.ly/5HywNG [...]
January 5, 2010 at 9:48 pm |
Is there a way to automatically make the edit page refresh? The problem I am having is the page loads before the workflows can finish. Normally if they refresh the page once the workflow has enough time to finish. All the Java script I have found puts the refresh on a cycle or a loop which will not work in this case.
January 6, 2010 at 11:59 pm
I have thought about this scenario and actually have this article half ready written already. I can not promise a publish date, but possibly during this weekend.
Alexander
January 11, 2010 at 9:12 am
You find the solution here
Alexander
January 18, 2010 at 4:41 am |
no sarcasm I understand script but how do i implement in edit or new page? I assume i unghost and where do I use in that in ghosted page?
T
January 18, 2010 at 1:24 pm
Hi,
As written in the article:
Add a CEWP below your list-form in NewForm or EditForm…
Click on the link on CEWP to read how you do this.
Alexander
January 31, 2010 at 3:44 pm |
Hi Alexander: I have been trying for 8 hours now to get this to work, but can’t. What I’ve noticed (by using alert messages) is that most of the variables are indeed set after adding a new item and transitioning back to the default list page. But the itemID variable is set to “Undefined”, which I’m guess is causing the query to fail when looking for the most recent new item by user, which then causes the whole re-direct to the edit form not to take place.
Any ideas why this is happening?
January 31, 2010 at 6:20 pm
Hi,
I guess this question is about this article:
Redirect from NewForm to EditForm or custom page.
Double check the List GUID, or use the lists displayname. Also try to put in an alert between line 15 and 16 like this:
alert(res.count);
If this returns “-1″ there is something wrong with the query.
Try this and get back to me if you do not get it.
Alexander
January 31, 2010 at 6:01 pm |
Nevermind, operator error
It turns out I had the wrong list GUID specified, which causes the code to quit, as intended.
I learned a good lesson in where to get the list GUID from your other posts, because before I was going into the list settings and grabbing it out of the URL bar, and then having to manually convert the html markup to the appropriate {‘s and -’s, which obviously I didn’t do correctly. Your tip about viewing the source code of the list page and searching for “listname” gives the exact GUID, already formatted correctly. Had I followed that tip in the first place, I would have saved myself an entire night’s sleep
Thanks again for the post–couldn’t have finished this project without it.
Best regards.
January 31, 2010 at 6:25 pm
Glad you figured it out – my answer above came a bit late…
Regards
Alexander
February 1, 2010 at 5:33 am |
Hmmm…just discovered that this doesn’t work for anonymous users because the _spUserId is undefined…
Know of any easy work-arounds? This is for a public-facing web site, so I have to be able to use it for anonymous users.
Thanks in advance.
February 1, 2010 at 7:18 pm
Hi,
Still on this article:
Redirect from NewForm to EditForm or custom page?
I guess you could query the list for the last added item, but this would not guarantee that you get the right item.
If you could generate some unique identifier and write to a (hidden) field in the form, you could query the list asking for the item containing your unique value.
My first thought would be to use the time stamp – it would be down to the millisecond, and therefore would be close to an guaranteed unique value.
Ask again if you need help adapting the function.
Alexander
October 4, 2010 at 8:13 pm |
Hi – is it possible to change text of OK button to “Submit”?
October 5, 2010 at 10:35 pm
Yes, like this:
$("input[id$='SaveItem']").val('Submit');Alexander
October 28, 2010 at 7:47 pm |
Thanks…very useful!!
December 8, 2010 at 9:11 pm |
Great post, just what the doctor ordered. Question… If I use this on an editform (for ID=45 or whatever) can I have the “OK” button take me back to the dispform for ID45 or just take me back 1 page, from wherever it was that I came?
Thanks again for the post.
December 9, 2010 at 6:16 pm |
After installing the CEWP and code I noticed that I was no longer able to delete the item from the dispform.aspx page.
If I clicked “Edit” I am able to delete the item on the editform.aspx page. I noticed the dispform.aspx URL ends in ID=XX while the editform.aspx ends in ID=xx&Source=%2FLists%2FCTL%2FMyItems%2Easpx.
suggestions?
April 3, 2011 at 4:38 am |
Is it possible to use this on a custom form? I can’t seem to figure it out?
April 3, 2011 at 5:40 am
Figured it out. Love the JQuery site.
var mySelectVal = $(‘input:radio[name=inputname]:checked’).val();
September 23, 2011 at 6:32 pm |
hey Alexander,
great code, works perfectly with the Save button, but i couldn’t make it work for the Cancel button.
I am trying to redirect to a custom page, rather than to its original page.
Can you please give me a sample code that will definitely help me.
Thanks in Advance…
alien
October 7, 2011 at 10:33 pm
Hi,
Sorry for the late reply.
The code example above has all the code needed:
// Where to go when cancel is clicked goToWhenCanceled = '/test/English/YouCanceled.aspx'; // Edit the redirect on the cancel-button's $('.ms-ButtonHeightWidth[id$="GoBack"]').each(function(){ $(this).click(function(){ STSNavigate(goToWhenCanceled); }) });This code example does not affet the ribbon-buttons in SharePoint 2010.
Alexander