Search code examples
javascriptsharepointsharepoint-2013sharepoint-designer

Saving survey response while redirecting user after clicking finish button in sharepoint


I have a question related to sharepoint 2013. I have a survey. At the last page, there is a yes/no question. All I want is that after clicking the "Finish" button provided by Sharepoint, the user should be redirected to a thankyou page. I am providing this via this code:

$(document).ready(function(){
    if(document.getElementById('ctl00_ctl31_g_78e43776_7d3b_4ab6_9d50_7801d0032f15_c
        window.location = "/SitePages/ThankYouE.aspx";
    }
});

However, the problem is the survey response is not saved because of redirection. When I remove redirection, saving process works well.

My question: How can I manage both saving the survey and redirecting the user after saving this?

I appreciate any helps.

Thanks


Solution

  • One way to do this would be to add some code to the overview.aspx page so that the user is redirected to a thank you page if he has already filled in the survey. Have a look at the code below that does exactly that.

    Some notes:

    • I've added some code to allow me to disable the redirect. This is useful for debugging because once I fill in the survey it would be difficult to see the Overview page again. To do this I'm checking if a "disableThankYou" query string paramter is defined and set to 1, in which case the thank you redirect logic does not work. (Source: How to get the value from the GET parameters?)
    • This parameter is also useful if I ever want the user to be able to view the Overview.aspx page again once he has filled in the survey. For example, we can have a button on the Thank You page that redirects to the "overview.aspx?disableThankYou=1" - that way if a user has already filled in the survey and navigates to the Overview page, he first gets redirected to the thank you page but has an option to go to the Overview page again.
    • I would also recommend that you hide the main overview div (or the whole page?) while the below code executes and only show it if the user is not redirected to the thank you page. This would make the user experience better as without it the user would see the Overview page contents flash before he is redirected to the Thank You page.

    Finally note some // TODOs comments in the code below which you may want to address.

    // TODO: Change this to the name of your survey.
    var listName = 'Test Survey';
    
    // TODO: Hide the Overview DIV or the whole page.
    
    
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', init);
    
    function init()
    {
      var clientContext = new SP.ClientContext.get_current();
        var oWeb = clientContext.get_web();
        var oList = clientContext.get_web().get_lists().getByTitle(listName);
    
        var camlQuery = new SP.CamlQuery();
        var query = "<View><Query><Where>" +
          "<Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Integer'>" + _spPageContextInfo.userId + "</Value></Eq>" +
          "</Where></Query></View>"
        camlQuery.set_viewXml(query);
        this.listItems = oList.getItems(camlQuery);
    
        clientContext.load(this.listItems);
    
        clientContext.executeQueryAsync(Function.createDelegate(this, onItemsLoaded), Function.createDelegate(this, onQueryFailed));        
     }       
    
    function onItemsLoaded(sender, args) 
    {
        if (QueryString.disableThankYou != 1 && this.listItems.get_count() == 1)
        {
          // TODO: Do your redirect here.
            alert('redirecting');
        }
        else
        {
          // TODO: Show the Overview DIV.
          alert('showing div');
        }
    }
    
    function onQueryFailed(sender, args) 
    {
    // Todo handle error
    alert('Error');
    }
    
    
    var QueryString = function () {
      // This function is anonymous, is executed immediately and 
      // the return value is assigned to QueryString!
      var query_string = {};
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
            // If first entry with this name
        if (typeof query_string[pair[0]] === "undefined") {
          query_string[pair[0]] = decodeURIComponent(pair[1]);
            // If second entry with this name
        } else if (typeof query_string[pair[0]] === "string") {
          var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
          query_string[pair[0]] = arr;
            // If third or later entry with this name
        } else {
          query_string[pair[0]].push(decodeURIComponent(pair[1]));
        }
      } 
        return query_string;
    }();