Custom redirect after creating a new Sharepoint Item

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.

24 Responses to “Custom redirect after creating a new Sharepoint Item”


  1. 1 bhildo February 4, 2011 at 8:45 pm

    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 });

  2. 2 UbuntuMan April 3, 2012 at 9:53 pm

    Kudos to you, Adrian!! Nice post. I’ve been looking for a long time for something like this. Thank you very much for posting.

  3. 3 Pedro January 11, 2013 at 12:00 pm

    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.

  4. 4 tabletr June 6, 2013 at 2:47 pm

    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

  5. 5 IPat June 6, 2013 at 9:49 pm

    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.

  6. 6 Fred July 21, 2014 at 5:16 pm

    Nope…doesn’t work….

  7. 7 Clem July 23, 2014 at 10:46 am

    throws web page cannot be found error in SP 2010.

  8. 8 Kelly August 11, 2014 at 4:54 pm

    Using the JavaScript example, it doesn’t save the item. Any clues why?

  9. 9 craftsman chainsaw parts primer bulb September 21, 2014 at 9:15 am

    My intent with this chain saw was for clearing the bottom” of vines, weeds, small timber at ground stage;..

    too big for gas trimmer brush blades and too little to reveal the gas chain saw blades to dust,
    and so on. Whereas on telephone with Craftsman Club, perceive a
    number of more have been ordered.

  10. 10 Timothy Dilbert February 16, 2015 at 6:28 am

    Unfortunately this does not work when the form is loaded in a Dialog box. Still searching for an answer on this..

  11. 11 Sush February 17, 2015 at 1:35 pm

    On the JQuery solution, please tell me where should I put the code. I’m a power user and uses SharePoint Designer 2013 for my SP 2013 sub site.

  12. 12 Francis Trujillo March 17, 2015 at 1:24 am

    Wow! The Source parameter. I was down a rabbit hole on PostBack heck.
    Thank you!! Works great!

  13. 13 karthikeyan baskaran July 8, 2015 at 2:49 pm

    Not working
    $(“form[name=aspnetForm]”).get(0).action;
    returns undefined

  14. 14 Mike W July 17, 2015 at 3:23 am

    Post is a little confusing. Please document what your code is doing.

  15. 15 Srini October 23, 2015 at 9:08 am

    $(document).ready(function() {

    var button = $(“input[id$=SaveItem]”);
    // change redirection behavior
    button.removeAttr(“onclick”);
    button.click(function() {
    var elementName = $(this).attr(“name”);
    var aspForm = document.forms[‘aspnetForm’];
    var oldPostbackUrl = aspForm.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));
    });

    });

    Above code worked for SP2013.

  16. 18 sharop December 24, 2015 at 2:38 am

    Hi, there is some variable where the new item id, is keeping? after postback. i need send to other form.

  17. 20 blues and rock June 4, 2016 at 10:36 pm

    It’s an remarkable post designed for all the web visitors; they will obtain benefit from it I
    am sure.

  18. 21 彼女 プレゼント 10万 June 5, 2016 at 6:47 am

    なりゆきの人生を送っている私。必要以上の金は持たねー。…って、ほざいてる場合ではない程貧乏でひどい。
    とはいえ行動力のみでここまで生きてきた。だから、今回の仰天企画も必ず成功させる。
    足りない金はプロミスで借入することにした。しかし、相手に嘘くさいことを喋ると落とされそうなので、そこだけは偏屈な俺でも慎重にやった。
    薔薇の花束を買う予定で最初は2万円もキャッシングできればいいと考えていた。だが嬉しい誤算で、限度額は何と15万円だった。これは印象に残るように10万円以上のものを買うしかない!と僕の中の別人格がつぶやいた。

  19. 22 流行 cm June 5, 2016 at 1:38 pm

    一頃はCMで大流行した無人契約機を使用して、手続きを行うという簡単な方法だってよく使われています。無人機を相手に自分で新規申込みを行うので、決して誰にも会わずに、急な出費があったときなどにカードローン申込をひっそりとすることができるので心配はありません。
    いわゆる貸金業関連の情報が検索できるHPを上手に活用すれば、素早い即日融資ができる消費者金融業者(いわゆるサラリーマン金融)を、手持ちのスマホから検索することも融資を申し込むことも問題ないのです。
    銀行の借入は、通常のキャッシングよりも年利が安いというメリットがあります。審査をした時の基準で、しっかり安定した収入のある人だけがカードローン利用をすることができますがパートや派遣社員の方も借入ができるようです。

  20. 23 technology July 8, 2016 at 4:02 pm

    Simply wish to say your article is as amazing. The clearness for your put up is just great and that
    i can think you’re a professional in this subject.
    Well with your permission let me to grasp your
    RSS feed to keep up to date with impending post.
    Thanks a million and please continue the gratifying work.

  21. 24 赤帽 見積もり September 14, 2016 at 9:20 am

    ある日本部長から呼ばれて移動なんて頻繁にある話です。ぺいぺいの立場だと避けられないから困りますよね。そんな時は引越料金くらいは安くしたいものです。そこで試したいのが赤帽やクロネコの小さな引越しです。一人でするという選択肢もありますが、やっぱり悩みます。業者に任せてお気軽に引越しするといいと思います。


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: