When creating a new Item in a Sharepoint list, the redirection works according to these rules:
- If the URL contains a valid “Source” parameter, the user user will be redirected to this URL
- Otherwise the user will be redirected to the default view of the list, such as AllItems.aspx
There are different approaches to change this behavior:
Via URL Parameter
One appraoch is to override the redirection by changing the Source parameter of the URL. This means the incoming link to the NewForm.aspx contains the redirection URL already. This is pretty static, sometimes you want to set the redirection on the fly. Also the appraoch enforces the same redirection for both Safe and Close buttons on the form.
Via Event Receiver
It is possible to redirect within an event receiver. This means placing a redirect command within a synchrounous event which aborts the normal event flow in a way, that asynchrounous events and other attached event receivers won’t work anymore.
Via Custom Safe Buttons
It is possible to replace the default buttons on the NewForm.aspx page by custom controls. So basically you develop a custom button that inherits from the Sharepoint “SaveButton” class. In there you can apply custom logic for redirection.
Via JavaScript
This is a novel approach I developed to get more flexibility and stability for the redirection behavior. It allows to set the redirection on the clientside.
$(document).ready(function() {
var button = $("input[id$=SaveItem]");
// change redirection behavior
button.removeAttr("onclick");
button.click(function() {
var elementName = $(this).attr("name");
var aspForm = $("form[name=aspnetForm]");
var oldPostbackUrl = aspForm.get(0).action;
var currentSourceValue = GetUrlKeyValue("Source", true, oldPostbackUrl);
var newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, "MyRedirectionDestination.aspx");
if (!PreSaveItem()) return false;
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true));
});
});
So basically we modify the click behavior of the create button on the NewForm.aspx to pass in a new “Source” parameter value just before the form is submitted.
Need help with above java script solution. Can you explain above solution line by line. Also where do i change your code to make it work. Also what is “SaveItem, name, aspnetform, source”? I have pasted your code below.
Thanks in advance
$(document).ready(function() {
02
03 var button = $(“input[id$=SaveItem]“);
04 // change redirection behavior
05 button.removeAttr(“onclick”);
06 button.click(function() {
07 var elementName = $(this).attr(“name”);
08 var aspForm = $(“form[name=aspnetForm]“);
09 var oldPostbackUrl = aspForm.get(0).action;
10 var currentSourceValue = GetUrlKeyValue(“Source”, true, oldPostbackUrl);
11 var newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, “MyRedirectionDestination.aspx”);
12
13 if (!PreSaveItem()) return false;
14 WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, “”, true, “”, newPostbackUrl, false, true));
15 });
16
17 });
Kudos to you, Adrian!! Nice post. I’ve been looking for a long time for something like this. Thank you very much for posting.
This is working great but it’s not working properly in Firefox and Chrome. In Firefox, when inserting an valid or invalid date, I get an unxpected error or this page has been modified error. In Chrome if I insert a valid data I can add the item, but if I insert an invalid date I get the same error that in Firefox. In IE we have no problems, do you an idea of what it could be? Thank you.
hi, i am not able to get the redirection poperrly.
am getting the error sourceurl is null and am unable to navigate to my site’s homepage.
Hello my family member! I wish to say that this post is amazing, nice written and include approximately all significant infos. I’d like to see extra posts like this .
Hey! This is my first visit to your blog! We are
a collection of volunteers and starting a new project in
a community in the same niche. Your blog provided us valuable information to work
on. You have done a wonderful job!
I pay a quick visit everyday some web sites and blogs to read posts, but
this web site offers quality based articles.
Hello! I could have sworn I’ve been to this blog before but after going through a few of the articles I realized it’s
new to me. Regardless, I’m certainly delighted I came across it and I’ll be book-marking it and checking back often!
Please help!
when i click button submits succesfull but not redirected.
Problem with modal dialog Windows. it says ‘window.frameElement’ is null or not an object
i cannot resolve
Thanks
sorry for my english
I would like to use this technique on an Edit form Save button. Is this possible? Also, in the return URL I wanted to add a filter based on a field I have just edited. Is this possible also?
Great post anyway many thanks.