0

Hi I am trying to insert the data in the form of multiple rows to a SharePoint list. I retrieved question number, question, choice answer options as shown in image. Now, I want to insert the question number, selected answer in a different list when the quiz is submitted. I have tried a lot. But, I am not getting any hint. Kindly suggest some answer to this problem.

quiz page

        var enumerator;
        var listItem;  
        var qNo;   
        var qst;   
        var optionA;            
        var optionB;
        var optionC;
        var optionD;


function retriveListItem()  
{      
    var siteUrl = '/sites/NewWorkSite';
    var context = new SP.ClientContext(siteUrl);  
    var list = context.get_web().get_lists().getByTitle('Questions1List');  
    var caml = new SP.CamlQuery();  
    caml.set_viewXml("<View><Query><OrderBy><FieldRef Name='QuestionNumber' Ascending='TRUE' /></OrderBy></Query></View>");  
    returnedItems = list.getItems(caml);  
    context.load(returnedItems);  
    context.executeQueryAsync(onSucceededCallback, onFailedCallback);  
}  

$(document).ready(function () 
{ ExecuteOrDelayUntilScriptLoaded(retriveListItem, "sp.js"); 
});


function onSucceededCallback(sender, args)  
{  
    enumerator = returnedItems.getEnumerator();  
    alert(enumerator);
    //Formulate HTML from the list items   
    var MainResult = 'Items in the Divisions list: <br><br>';  
    //Loop through all the items   
    while(enumerator.moveNext())  
    {  

        listItem = enumerator.get_current();  
        qNo = listItem.get_item("QuestionNumber");   
        qst = listItem.get_item("Question");   
        optionA = listItem.get_item("OptionA");         
        optionB = listItem.get_item("OptionB");
        optionC = listItem.get_item("OptionC");
        optionD = listItem.get_item("OptionD");



                $('#qDiv').append("<label>" + qNo+ "." + "</label>&nbsp" + "<label id='qts'>" + qst + "</label>" + "</br>" 
                + "<label>A: </label>&nbsp" + "<input id='one' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'A'/>&nbsp" + optionA  + "&nbsp</br>"
                + "<label>B: </label>&nbsp" + "<input id='two' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'B'/>&nbsp" + optionB + "</br>"
                + "<label>C: </label>&nbsp" + "<input id='three' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'C'/>&nbsp" + optionC + "</br>"
                + "<label>D: </label>&nbsp" + "<input id='four' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'D'/>&nbsp" + optionD + "</br>" + "</br>");

    }                             
}  
        //This function fires when the query fails   
function onFailedCallback(sender, args)  
{  
        //Formulate HTML to display details of the error   
        var markup = '<p>The request failed: <br>';  
        markup1 = 'Message: ' + args.get_message() + '<br>';  
        //Display the details   
        alert(markup1);
} 


// Adding list items code starts here

function addListItem()  
{  
var length = count;
var listTitle = "StudentList";  
//Get the current client context  
context = SP.ClientContext.get_current();  
var airportList = context.get_web().get_lists().getByTitle(listTitle);  
//Create a new record  
var listItemCreationInformation = new SP.ListItemCreationInformation();  
var listItem = airportList.addItem(listItemCreationInformation);  
//Set the values  
var sudID = $("#stID").val();
var optionValue = $('input[name ="'+ qNo +'"]:checked').val();

while(enumerator.moveNext())
{
    listItem.set_item('StudentID', + sudID );  
    listItem.set_item('QuestionNumber', + qNo);
    listItem.set_item('Question', + qst); 
    listItem.set_item('Answer', + optionValue);     
    listItem.update();  
    context.load(listItem);  
    context.executeQueryAsync(addListItemSucceeded, addListItemFailed);
}
}  

function addListItemSucceeded()  
{  
retriveListItem();  
}  

function addListItemFailed(sender, args)  
{  
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.