This post going to describe that how to Insert, Update and Delete list items using ECMA Script(JavaScript Client Object Model).
Insert List Item
var nmspCon = {};
nmspCon.varEmpName = "";
nmspCon.varEmpAddress = "";
nmspCon.varEmpCity = "";
nmspCon.varEmpIdCardNo = "";
nmspCon.fncSaveForm = function () {
nmspCon.varEmpName = $('#txtName').val();
nmspCon.varEmpAddress = $('#txtAddress').val();
nmspCon.varEmpCity = $('#txtCity').val();
nmspCon.varEmpIdCardNo = $('#txtIdCardNo').val();
nmspCon.clientContext = SP.ClientContext.get_current();
nmspCon.oList = nmspCon.clientContext.get_web().get_lists().getByTitle('Employee Information');
nmspCon.itemCreateInfo = new SP.ListItemCreationInformation();
nmspCon.oListItem = nmspCon.oList.addItem(nmspCon.itemCreateInfo);
nmspCon.oListItem.set_item('Name', nmspCon.varEmpName);
nmspCon.oListItem.set_item('Address', nmspCon.varEmpAddress);
nmspCon.oListItem.set_item('City', nmspCon.varEmpCity);
nmspCon.oListItem.set_item('IdentityCardNumber', nmspCon.varEmpIdCardNo);
nmspCon.oListItem.update();
nmspCon.clientContext.load(nmspCon.oListItem);
nmspCon.clientContext.executeQueryAsync(
Function.createDelegate(this, nmspCon.onQuerySuccess),
Function.createDelegate(this, nmspCon.onQueryFail)
);
}
nmspCon.onQuerySuccess = function () {
alert("Success.!");}
nmspCon.onQueryFail = function (sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());}
Update List Item
nmspCon.varType = "Temporary";
nmspCon.clientContext = SP.ClientContext.get_current();
nmspCon.oList = nmspCon.clientContext.get_web().get_lists().getByTitle('Employee Information');
nmspCon.listItem = nmspCon.oList.getItemById(1);
nmspCon.listItem.set_item('Type', '' + nmspCon.varType);
nmspCon.listItem.update();
nmspCon.clientContext.load(nmspCon.listItem);
nmspCon.clientContext.executeQueryAsync(
Function.createDelegate(this, nmspCon.onQuerySuccess),
Function.createDelegate(this, nmspCon.onQueryFail));
}
nmspCon.onQuerySuccess = function () {
alert("Success.!");}
nmspCon.onQueryFail = function () {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());}
Delete List Item
nmspCon.clientContext = SP.ClientContext.get_current();
nmspCon.oList = nmspCon.clientContext.get_web().get_lists().getByTitle('Employee Information');
nmspCon.listItem = nmspCon.oList.getItemById(1);
nmspCon.listItem.deleteObject();
nmspCon.clientContext.load(nmspCon.listItem);
nmspCon.clientContext.executeQueryAsync(
Function.createDelegate(this, nmspCon.onQuerySuccess),
Function.createDelegate(this, nmspCon.onQueryFail));
}
nmspCon.onQuerySuccess = function () {
alert("Success.!");}
nmspCon.onQueryFail = function () {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());}
No comments:
Post a Comment