Ads

Friday, 17 March 2017

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>

No comments:

Post a Comment

Ads