magritton
12/9/2015 - 4:16 PM

JavaScript example of saving data to a SharePoint list. This iterates through a collection of found elements and then writes that data to th

JavaScript example of saving data to a SharePoint list. This iterates through a collection of found elements and then writes that data to the SharePoint list.

function writeToMaterList(itemGuid,uName, uDept)
{
	var clientContext= new SP.ClientContext.get_current();
	var oList = clientContext.get_web().get_lists().getByTitle('Red Flag Checklist');
	var itemCreateInfo = new SP.ListItemCreationInformation();
    var oListItem = oList.addItem(itemCreateInfo);
    
    var strLink = 'http://scushp01/Teams/compliance/Lists/RedFlagData/RegFlagEntry.aspx?ItemKey=' + itemGuid;
    oListItem.set_item('Member_x0020_Name', uName);
    oListItem.set_item('Department', uDept);
    oListItem.set_item('ItemKey', itemGuid);
    var urlValue = new SP.FieldUrlValue();
  	urlValue.set_url(strLink);
  	urlValue.set_description("Edit Item");
    oListItem.set_item('EntryLink', urlValue);
    oListItem.update();
    clientContext.load(oListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQueryMasterSucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQueryMasterSucceeded() {
    console.log('Master Item created');
    window.location.href = 'http://scushp01/Teams/compliance/Pages/RFC.aspx';
}
function saveToList()
{
	var clientContext= new SP.ClientContext.get_current();
	var oList = clientContext.get_web().get_lists().getByTitle('RedFlagData');
    
	//var table = document.getElementById('TableofValues');
	//var rowLength = table.rows.length;
	var itemGuid = NewGuid();
	
	for(var i=1; i<38; i++){
	  	var row = document.getElementById('Row' + i);
	  	var cc = row.cells[1].getAttribute('cc');
	  	var Descrip = row.cells[2];
	  	var dd1 = $("#" + row.cells[3].firstChild.id).jqxDropDownList('getSelectedItem');
	  	var dd2 = $("#" + row.cells[4].firstChild.id).jqxDropDownList('getSelectedItem');
	  	var dd3 = $("#" + row.cells[5].firstChild.id).jqxDropDownList('getSelectedItem');
	  	var tt1 = $('#' + row.cells[6].firstChild.id).jqxTextArea('val');
	  	var tt2 = $('#' + row.cells[7].firstChild.id).jqxTextArea('val');
	  	var tt3 = $('#' + row.cells[8].firstChild.id).jqxTextArea('val');
	  	var tt4 = $('#' + row.cells[9].firstChild.id).jqxTextArea('val');
	  	var dd4 = $("#" + row.cells[10].firstChild.id).jqxDropDownList('getSelectedItem');
	  	var dd5 = $("#" + row.cells[11].firstChild.id).jqxDropDownList('getSelectedItem');
	  	var dd6 = $("#" + row.cells[12].firstChild.id).jqxDropDownList('getSelectedItem');
	  	var dd7 = $("#" + row.cells[13].firstChild.id).jqxDropDownList('getSelectedItem');
	  	var uName = document.getElementById('UserName');
	  	var uDept = document.getElementById('UserDepartment');
	  	//var ley = NewGuid();
	  	var itemCreateInfo = new SP.ListItemCreationInformation();
        var oListItem = oList.addItem(itemCreateInfo);
        
    	oListItem.set_item('Code_x0020_Category_x0020_Descri', Descrip.innerHTML);
    	oListItem.set_item('Code_x0020_Category', cc);
    	oListItem.set_item('Detection', dd1.label);
    	oListItem.set_item('Mitigation', dd2.label);
    	oListItem.set_item('Response', dd3.label);
    	oListItem.set_item('Policy_x002d_Procedure', tt1);
    	oListItem.set_item('Gap_x0020_Analysis', tt2);
    	oListItem.set_item('Identified_x0020_Inherent_x0020_', tt3);
    	oListItem.set_item('Risk_x0020_Mitigation_x0020_and_', tt4);
    	oListItem.set_item('Control_x0020_Type', dd1.label);
    	oListItem.set_item('Impact', dd2.label);
    	oListItem.set_item('Likelihood', dd3.label);
    	oListItem.set_item('Risk_x0020_Indicator', dd4.label);
    	oListItem.set_item('Completed_x0020_By', uName.innerHTML);
    	oListItem.set_item('Department', uDept.innerHTML);
		oListItem.set_item('IDKey',itemGuid);
		oListItem.set_item('RowKey','Row' + i);
    	oListItem.update();
    	clientContext.load(oListItem);
    	clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
  	  	var cellLength = row.cells.length;  	
	}//ends the first for
	writeToMaterList(itemGuid,uName.innerHTML,uDept.innerHTML);
	
}