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.
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.
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.
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:
2) copy the URL and change the part after Source= to the desired destination. The following will send them back to your home page:
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:
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:
- Go the “New Item” page
- 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
- Click “Add a Web Part” and add a Content Editor Web Part (CEWP)
- Move the CEWP below the existing list web part
- Edit the CEWP and click the Source Editor button
- Paste the JavaScript from below
- Save your changes and exit the edit mode of the page
- 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
- 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:
Wow it just solved my problem.
Thanks a bunch
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?
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
Hi Mike,
What do you do with the URL in Step 8 (Redirect Cancel Button)?
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
Thank you very much!
Your hint was very important for my situation.
This worked a treat on my new Sharepoint Website design. Thanks ever so much.
Great, after reading this I understand how to use the CEWP for other javascript functions in the future.
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
Legend- Finality i understand the Source and why forms have a larger URL then Sites.
Thank you sir
I use the InfoPath Web Part Form and it doesn't work for me..please help
Great! Thanks so much!
Thanks for the post.
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?
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
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
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
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