Monday, April 30, 2012

Creating a back button on a SharePoint 2010 survey

The best way I've found to create a back button on a SharePoint 2010 survey is to use javascript (with JQuery) to add a html input button and add the onClick event function there.
Though users can easily click the back button on their browser, SharePoint will not let them click the 'Next' button as it will throw the following exception.

Save Conflict.
Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes.



My code below is in the EditForm.aspx file of the survey list. You can find and edit it using SharePoint designer.
The code works in the following steps:
  1. Adds the back button to the page
  2. When the button is clicked it sets the 'backClicked' cookie to 'yes' then triggers the 'Next' button. Triggering the 'Next' button causes the current questions to post back to the server and save the input.
  3. Everytime the page is loaded the code checks the 'backClicked' cookie. If it is set to 'yes' it will then set it to 'needRefresh' and will tell the browsers to go back twice "history.go(-2);".
  4. Now the browser is on the correct page of the survey the code checking the 'backClicked' cookie will see that it is set to 'needRefresh', set the cookie to 'no' then cause the browser to reload the page "location.reload();".
  5. It is important that the browser is refreshed on the same page. This resolves the Save Conflict issue mentioned above.

 I hope this helps someone out there. If you know of any alternative solutions, please post in the comments.