Update SPListItem with JavaScript
I was creating a custom action to update the selected items.
You would think - really easy right - well not for me :-( Hope following JavaScript code helps for you though.
var ctx;
var notifyId = '';
function updateListItem(){
this.notifyId = SP.UI.Notify.addNotification('Updating items...', true);
// Get the current ClientContext
this.ctx = new SP.ClientContext.get_current();
// Get the current Web
var web = this.ctx.get_web();
// Get the currently selected list
var listId = SP.ListOperation.Selection.getSelectedList();
var sourceList = web.get_lists().getById(listId);
// Get the selected items and update each of them
var items = SP.ListOperation.Selection.getSelectedItems(this.ctx);
if(confirm('Are you sure?')){
for(var i = 0; items.length > i; i++) {
var listItem = sourceList.getItemById(items[i].id);
this.ctx.load(listItem);
listItem.set_item('EUTTT_SP_IsIdentified', 'Identified');
listItem.update();
}
}
else {
for(var i = 0; items.length > i; i++) {
var listItem = sourceList.getItemById(items[i].id);
this.ctx.load(listItem);
listItem.set_item('EUTTT_SP_IsIdentified', 'Abandoned');
listItem.update();
}
}
this.ctx.executeQueryAsync( Function.createDelegate(this, onUpdateSucceeded), Function.createDelegate(this, onQueryFailed));
}
function onUpdateSucceeded(sender, args) {
SP.UI.Notify.removeNotification(this.notifyId);
SP.UI.ModalDialog.RefreshPage( SP.UI.DialogResult.OK);
}
function onQueryFailed(sender, args) {
SP.UI.Notify.removeNotification(this.notifyId);
SP.UI.Notify.addNotification('The update operation failed.', true);
}
updateListItem();
You would think - really easy right - well not for me :-( Hope following JavaScript code helps for you though.
var ctx;
var notifyId = '';
function updateListItem(){
this.notifyId = SP.UI.Notify.addNotification('Updating items...', true);
// Get the current ClientContext
this.ctx = new SP.ClientContext.get_current();
// Get the current Web
var web = this.ctx.get_web();
// Get the currently selected list
var listId = SP.ListOperation.Selection.getSelectedList();
var sourceList = web.get_lists().getById(listId);
// Get the selected items and update each of them
var items = SP.ListOperation.Selection.getSelectedItems(this.ctx);
if(confirm('Are you sure?')){
for(var i = 0; items.length > i; i++) {
var listItem = sourceList.getItemById(items[i].id);
this.ctx.load(listItem);
listItem.set_item('EUTTT_SP_IsIdentified', 'Identified');
listItem.update();
}
}
else {
for(var i = 0; items.length > i; i++) {
var listItem = sourceList.getItemById(items[i].id);
this.ctx.load(listItem);
listItem.set_item('EUTTT_SP_IsIdentified', 'Abandoned');
listItem.update();
}
}
this.ctx.executeQueryAsync( Function.createDelegate(this, onUpdateSucceeded), Function.createDelegate(this, onQueryFailed));
}
function onUpdateSucceeded(sender, args) {
SP.UI.Notify.removeNotification(this.notifyId);
SP.UI.ModalDialog.RefreshPage( SP.UI.DialogResult.OK);
}
function onQueryFailed(sender, args) {
SP.UI.Notify.removeNotification(this.notifyId);
SP.UI.Notify.addNotification('The update operation failed.', true);
}
updateListItem();
Interesting.
BeantwoordenVerwijderenPs. For all your IT-vacancies, check out ictvacature .