Thursday, July 31, 2014

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:

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:

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>


Reference:

No comments:

Post a Comment

Image noise comparison methods

 1. using reference image technique     - peak_signal_noise_ratio (PSNR)     - SSI 2. non-reference image technique     - BRISQUE python pac...