The list schema for survey list is quite different from the custom lists. Every question is added as the list column(field) and every response to questions is added as list item. To retrieve the survey question and and to submit the response to list via client object model, do the following.
1)Load the reference of sp.js and Load the address of function to be called as below
SP.SOD.executeOrDelayUntilScriptLoaded(DisplaySurveyQ,’SP.js’);
2)Define the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
var surveyItems = null; var surveyFields=null; var surveyList = null; var oneField = null; function DisplaySurveyQ() { var context = new SP.ClientContext.get_current(); var web = context.get_web(); // of your desired target list: surveyList = web.get_lists().getByTitle('surveylistName'); //alert(surveyList); var caml = new SP.CamlQuery(); surveyItems = surveyList.getItems(caml); surveyFields=surveyList.get_fields(); oneField = surveyFields.getByInternalNameOrTitle("Title"); //alert(surveyFields); //alert(surveyItems); //listOperations context.load(oneField); context.load(surveyList); context.load(this.surveyItems); context.load(this.surveyFields); context.load(list); alert(list); |
3) Create callback handlers in this function
1 2 |
var success = Function.createDelegate(this, this.onSuccess); var failure = Function.createDelegate(this, this.onFailure); |
4)Execute an async query in this function
1 2 |
context.executeQueryAsync(success,failure); } |
5)Define Function On success full operation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// Async query succeeded. function onSuccess(sender, args) { // for example iterate through list items or fields and assign the value to html controls like div, uls etc var listEnumerator = surveyItems.getEnumerator(); var listFieldEnumerator = surveyFields.getEnumerator(); var Fields=surveyList.get_fields(); var Questions=new Array(); var qCount=0; //iterate though all of the Questions and get the recently added question from Survey var Count=0; while (listFieldEnumerator .moveNext()) { var currentField=listFieldEnumerator.get_current(); //alert(currentField.get_fieldTypeKind()); if(currentField.get_fieldTypeKind()==SP.FieldType.choice) { //alert(currentField.get_title()); Questions[qCount]=currentField; } Count= Count+ 1; } var currentField=Questions[qCount]; var FieldsChoices=currentField.get_choices() ; // save the current questions internal name in hidden field , which will be helpful while submitting the response. input type='hidden' name='CurrentQuestion' id='CurrentQuestion' value='"+currentField.get_internalName() var divSurvey='generateRequiredHtml' document.getElementById('generateRequiredHtmlandAssignThisIDmustPresent').innerHTML =divSurvey ; } |
6)Pass on the exception to UI by defining the failure function
1 2 3 4 5 |
// Async query failed. function onFailure(sender, args) { alert(args.get_message()); } |
7)To Submit Response to Survey List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
function SubmitVote() { //Get the Group of radio Buttons for vote options and get the selected value //alert($("input[name='grpRadio']:checked").val()); if (undefined === $("input[name='grpRadio']:checked").val()) { // do something alert('Please select any response'); return; } // Get the current context var context = new SP.ClientContext.get_current(); // Get the current site (SPWeb) var web = context.get_web(); // Get the survey list var list = web.get_lists().getByTitle('surveyListname'); var itemCreateInfo = new SP.ListItemCreationInformation(); var listItem = list.addItem(itemCreateInfo); var response=document.getElementById('SelectedRadio').value;(response stored in hidden field) var questionId=document.getElementById('CurrentQuestion').value (question's internal name stored in hidden field) /* Set fields in the item. In managed code, this would be listItem[fieldname] = value. In Javascript, call listItem.set_item(fieldName,value). */ listItem.set_item(questionId, response); listItem.update(); // Create callback handlers var success = Function.createDelegate(this, this.onSuccessVote); var failure = Function.createDelegate(this, this.onFailureVote); // Execute an async query context.executeQueryAsync(success,failure); } // Async query succeeded. function onSuccessVote(sender, args) { alert('Thanks for responding Survey'); // Refresh the page. // SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK); } // Async query failed. function onFailureVote(sender, args) { alert(args.get_message()); } |
8)Add the above code lines in txt file as follows
1 2 3 4 5 |
///less than sign![CDATA[ -- above code lines here-- ]]grater than sign script ends //static html here inside div div to assign generated html) |
9)To debug above code place debugger; after any code line,which helps you to debug client object code with Visual Studio debugger.
10)Save this file as .txt file and upload to library and refer this link in content editor webpart
This post is for good maintainability of code on Sharepoint site itself.
Hopefully it will help you. Happy coding.
Best SharePoint Hosting Recommendation
ASPHostPortal.com provides its customers with Plesk Panel, one of the most popular and stable control panels for Windows hosting, as free. You could also see the latest .NET framework, a crazy amount of functionality as well as Large disk space, bandwidth, MSSQL databases and more. All those give people the convenience to build up a powerful site in Windows server. ASPHostPortal.com offers SharePoint hosting starts from $9.99. We also guarantees 30 days money back and guarantee 99.9% uptime. If you need a reliable affordable SharePoint Hosting, we should be your best choice.