Pages

6/27/2009

SharePoint: Redirecting to a page after adding a New item to a list

 

There are times when you want a user to add an item to a list without displaying the list, either before or after they fill out the “New Item” form.

 image

To get them to the form is easy. Go to the list, click the New button and note the URL. Copy the URL and paste it as a link in Quick Launch, an Announcement, a links list or a Content Editor Web Part.

image

When the user clicks your link they will go directly to the New item page. But… when they click OK they will go to the list, not back to your page.

 image

 

The fix is easy...        Add “source=” to the end of the link.

 

1) Go to the list and click the new button. You will see a URL like:

http://www.yourserver.com/sites/sales/Lists/Tasks/NewForm.aspx?RootFolder=%2FLists%2FTasks&Source=http%3A%2F%2Fwww.yourserver.com%2Fsites%2Fsales%2FLists%2FTasks%2FAllItems%2Easpx


2) copy the URL and change the part after Source= to the desired destination. The following will send them back to your home page:

http://www.yourserver.com/sites/sales/Lists/Tasks/NewForm.aspx?RootFolder=%2FLists%2FTasks&Source=http%3A%2F%2Fwww.yourserver.com%2Fsites%2Fsales

 

Absolute or Relative?

Best practice would be to use a relative URL (no HTTP or server name) so your site will work when you have internet and intranet access to the same site. Something like "Source=/sites/sales/" or "Source=/sites/sales/mycustompage.aspx".

To return to the home page add this URL as a link in your Announcement, a links list or a Content Editor Web Part:

Absolute:

http://www.yourserver.com/sites/sales/Lists/Tasks/NewForm.aspx?RootFolder=%2FLists%2FTasks&Source=http%3A%2F%2Fwww.yourserver.com%2Fsites%2Fsales

Relative:

/sites/sales/Lists/Tasks/NewForm.aspx?RootFolder=%2FLists%2FTasks&Source=%2Fsites%2Fsales


In the examples above I did not always escape the slashes:  /  =  %2F
Any slash after the "?" should be escaped.

 

 

Redirect the Cancel Button

One of the questions below notes that the redirect steps above redirect both the OK and the Cancel button to the “Source” URL.  Here’s is a quick pass at adding a “CancelDestination” parameter to the query string to set the destination for the cancel button.

Steps:

  1. Go the “New Item” page
  2. Edit the URL and add “&ToolPaneView=2” to the end of the URL to put the page in edit mode
     
    http://yourserver/sites/sales/Lists/Announcements/NewForm.aspx?RootFolder=%2fsites%2fsales%2fLists%2fAnnouncements&Source=http%3a%2f%2fyourserver%2fsales%2fyoursite&ToolPaneView=2
     
  3. Click “Add a Web Part” and add a Content Editor Web Part (CEWP)
  4. Move the CEWP below the existing list web part
  5. Edit the CEWP and click the Source Editor button
  6. Paste the JavaScript from below
  7. Save your changes and exit the edit mode of the page
  8. Create a URL similar to the one earlier in the article, but add a new parameter named “&CancelDestination=http://yourcanceldestination"
     
    http://www.yourserver.com/sites/sales/Lists/Tasks/NewForm.aspx?RootFolder=%2FLists%2FTasks&Source=http%3A%2F%2Fwww.yourserver.com%2Fsites%2Fsales&CancelDestination=http%3A%2F%2Fwww.yourserver.com%2Fsites%2Fsales%2FCancelPage.aspx
     
  9. Test!

 

The JavaScript:

<script>

var querystring = window.location.search.substring(1);  
var parameters = querystring.split("&");
var QueryString = Array();
for (var i=0;i<parameters.length;i++)
{
  QueryString[parameters[i].split("=")[0]] = parameters[i].split("=")[1];
}
if (QueryString["CancelDestination"] != undefined)
{
  var inputs = document.getElementsByTagName("INPUT");
  for (var i=0; i<inputs.length; i++)
  {
    if (inputs[i].value == "Cancel")
    {
      inputs[i].onclick =
function () {document.location.href = unescape(QueryString["CancelDestination"])}; } } } </script>

 

.

18 comments:

Anonymous said...

Wow it just solved my problem.
Thanks a bunch

Unknown said...

Wonderful! Finally got this to work with the help of this article. One more question though: I have them redirected to a "Thank You" page, and I just tested the "Cancel" button, which also gets redirected to the Thank you page. Any work around in mind for that?

Mike Smith said...

Terri-Lynn,

Not with the simple trick above. Both the OK and Cancel buttons go to the same destination.

We can modify the page and add a little JavaScript to change the cancel behavior. I'll try to add that to the article later today.

Mike

Lisa Romie said...

Hi Mike,

What do you do with the URL in Step 8 (Redirect Cancel Button)?

Mike Smith said...

Lisa,

> What do you do with the URL in Step 8 (Redirect Cancel Button)?

Put anywhere you like. You could use it as the link in Quick Launch. You could also use it in an announcement as a hyperlink or you could send it as a link in an email.

Mike

Anonymous said...

Thank you very much!
Your hint was very important for my situation.

Anonymous said...

This worked a treat on my new Sharepoint Website design. Thanks ever so much.

Anonymous said...

Great, after reading this I understand how to use the CEWP for other javascript functions in the future.

Unknown said...

dude You are a legend- i always looked at the Source item and wondered why forms and lists have such a large URL now i notice the source is a redirect code- MY GOODNESS- legend

Unknown said...

Legend- Finality i understand the Source and why forms have a larger URL then Sites.

Thank you sir

Anonymous said...

I use the InfoPath Web Part Form and it doesn't work for me..please help

Suzette said...

Great! Thanks so much!

Ram Sampath said...

Thanks for the post.

Ben Jones said...

Mike, I'm trying to get this working on a two page survey. I'm using your trick to redirect the cancel button to redirect "cancel," "finish," and "save and close" with a few "else if" statements added to your javascript.

All works well on the first page of the form, but clicking "next" modifies my URL and blows out my "cancel destination" query string. Thus, even though I've added a CEWP with your script to my "EditItem" page, I lose the query string that triggers the script.

Any thoughts on how I can overcome this?

Mike Smith said...

Ben,

I'd have to think about it a bit, but I think the fix is to append the original "cancel destination" query string to the Next button's onclick event so the next page (EditItem) can pick it up. If that won't work then you would need to create a cookie in the first page to store the "cancel destination" and read it from the second page.

Mike

Anonymous said...

Hi Mike,
This option works perfectly for one of my sites but another that has a very long url, it is not working because of the character limitation in the path. Any ideas on how can redirect a user after submitting feedback through an InfoPath form back to the homepage and not to the feedback list itself?

Thanks
Daphne

Anonymous said...

Mike,

I used this but it recently stopped working. I've done a ton of research on WHY it would stop and can't find any - URL deleted - that's my URL and its not redirecting - just staying on the This form is closed paged...any ideas?

For those that want a redirect to a Thank you page and cancel - I always make my Thank you page a VIEW in my infopath form....so it switches to that view vs redirected - this keeps cancel or close going back to where you want it. However, mine has broken and don't know why.

TheCoupDiva

Mike Smith said...

TheCoupDiva,

I edited your post to remove the URL...

The issue may be that the redirect no longer works with InfoPath Forms Services. Have you applied any recent service packs or patches?

See if anything in this article helps: https://askmanisha.wordpress.com/2013/08/20/redirection-to-a-page-on-infopath-form-closure-inside-an-infopath-form-web-part/

You also might try a relative URL. /hq/10a/cois/OCM and see if that helps.

Mike

Post a Comment

Note to spammers...
Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.