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:

    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;
    }();