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.

About these ads

29 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 prasadwt March 4, 2013 at 11:21 am

    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.

  5. 5 hack league of legends, hack lol, hack lol tools, hacking lol, download lol tools, download lol hacks April 15, 2013 at 12:48 pm

    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 .

  6. 6 click to more April 27, 2013 at 8:43 pm

    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!

  7. 7 freeconsumerreview.com May 3, 2013 at 4:08 am

    I pay a quick visit everyday some web sites and blogs to read posts, but
    this web site offers quality based articles.

  8. 8 union square christmas tree May 15, 2013 at 12:15 am

    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!

  9. 9 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

  10. 10 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.

  11. 11 Hearthstone card hack February 6, 2014 at 2:34 pm

    Nice post. I was checking continuously this blog and
    I’m impressed! Extremely helpful information particularly the last part :
    ) I care for such information a lot. I was looking for this
    particular info for a very long time. Thank you and good luck.

  12. 12 Alloy wheel refurb Barnsley April 21, 2014 at 12:54 am

    Its such as you learn my mind! You appear to know a lot about this,
    such as you wrote the e book in it or something.

    I believe that you just can do with some p.c. to pressure the
    message home a little bit, however instead of that, that is
    magnificent blog. A great read. I’ll certainly be back.

  13. 13 www July 19, 2014 at 9:40 pm

    Spot on with this write-up, I actually feel this website needs a great deal more
    attention. I’ll probably be back again to read more, thanks for
    the advice!

  14. 14 Minecraft Cracked July 21, 2014 at 12:39 pm

    I’m impressed, I have to admit. Rarely do I encounter a blog that’s both educative and amusing,
    and let me tell you, you have hit the nail on the head.
    The problem is something too few folks are speaking intelligently about.

    Now i’m very happy I found this in my hunt for something relating to this.

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

    Nope…doesn’t work….

  16. 16 Nedivekt.Com July 21, 2014 at 5:42 pm

    Hi there mates, how is everything, and what you desire to say regarding this article, in my view its actually
    amazing in favor of me.

  17. 17 GåNedivekt.Com July 21, 2014 at 7:33 pm

    Good article! We will be linking to this particularly great article on our
    site. Keep up the great writing.

  18. 18 Gånedivekt.com July 22, 2014 at 10:47 am

    you’re in point of fact a just right webmaster. The site loading velocity is amazing.
    It kind of feels that you are doing any distinctive trick.
    Also, The contents are masterpiece. you’ve done a excellent job in this matter!

  19. 19 Kazuko July 22, 2014 at 5:45 pm

    whoah this weblog is great i really like studying your posts.
    Keep up the great work! You recognize, lots of persons are searching around
    for this information, you can aid them greatly.

  20. 20 Http://GåNedivekt.com/bli-tynn/top-8-bli-tynn-pa-3-Uker/ July 22, 2014 at 10:10 pm

    Simply wish to say your article is as astonishing. The clarity in your post is just excellent and i could assume you are an expert on this subject.

    Well with your permission let me to grab your feed to keep updated with forthcoming post.

    Thanks a million and please keep up the gratifying work.

  21. 21 nedivekt.Com July 23, 2014 at 5:12 am

    I have read so many articles concerning the blogger lovers however this paragraph is truly a fastidious piece of writing, keep it up.

  22. 22 Http://GåNedivekt.Com/Slanketips/Naturlige-Slanketips-Slankeguiden/ July 23, 2014 at 5:42 am

    It’s truly a great and helpful piece of information. I’m glad that
    you just shared this helpful information with us.
    Please stay us informed like this. Thanks for sharing.

  23. 23 http://gåNedivekt.com/ July 23, 2014 at 8:39 am

    I do believe all of the ideas you have presented to your post.
    They are really convincing and will certainly work.
    Nonetheless, the posts are very brief for newbies.
    May you please prolong them a little from subsequent time?

    Thank you for the post.

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

    throws web page cannot be found error in SP 2010.

  25. 25 gånedivekt.Com July 23, 2014 at 11:02 am

    I loved as much as you’ll receive carried out right here.
    The sketch is tasteful, your authored subject matter
    stylish. nonetheless, you command get bought an impatience
    over that you wish be delivering the following. unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this increase.

  26. 26 Hvordan bli tynn blogg July 23, 2014 at 7:35 pm

    You actually make it seem really easy along with your presentation but I in finding this matter to be actually something which I feel I would by
    no means understand. It seems too complicated and very broad for me.
    I’m having a look ahead on your next put up, I will attempt to get the grasp of it!

  27. 27 pooltable moving In New jersey July 24, 2014 at 2:03 pm

    It’s a pity you don’t have a donate button! I’d most certainly donate to this outstanding blog!
    I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google
    account. I look forward to new updates and will talk about this website with my Facebook
    group. Chat soon!

  28. 28 Tresa August 2, 2014 at 8:48 am

    Hi i am kavin, its my first occasion to commenting anyplace, when i read
    this piece of writing i thought i could also make comment
    due to this brilliant article.

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

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


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





Follow

Get every new post delivered to your Inbox.

%d bloggers like this: