Solución:
Tengo una lista llamada rohit_list1
en el que estoy actualizando title
para todos los artículos con ID
mas grande que 15
. Escribe su propia consulta CAML para obtener ID.
Diseño :
<input type="button" value="updateMultipleListItems" onclick="getItemsToBeUpdated()" />
Código JSOM:
<script type="text/javascript">
var collListItemToBeUpdated = "";
var listItemToBeUpdated = "";
function getItemsToBeUpdated() <--- this function called on button click to get ID's
{
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('rohit_list1');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Geq>' +
'<FieldRef Name='ID'/><Value Type='Number'>15</Value>' +
'</Geq></Where></Query></View>');
collListItemToBeUpdated = oList.getItems(camlQuery);
clientContext.load(collListItemToBeUpdated);
clientContext.executeQueryAsync(getItemsToBeUpdatedSuccess, getItemsToBeUpdatedFailed);
}
function getItemsToBeUpdatedSuccess()
{
ListItemToBeUpdated= collListItemToBeUpdated.getEnumerator();
updateMultipleListItems();
}
function getItemsToBeUpdatedFailed(sender, args)
{
alert('Request failed. ' + args.get_message() + 'n' + args.get_stackTrace());
}
function updateMultipleListItems() <--- this function called on successfully getting ID's
{
var itemArray = [];
var clientContext = SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('rohit_list1');
while(ListItemToBeUpdated.moveNext())
{
var oItem = ListItemToBeUpdated.get_current();
var oListItem = oList.getItemById(oItem.get_id());
oListItem.set_item('Title', 'Updated ...!');
oListItem.update();
itemArray.push(oListItem);
clientContext.load(itemArray[itemArray.length-1]);
}
clientContext.executeQueryAsync(updateMultipleListItemsSuccess, updateMultipleListItemsFailed);
}
function updateMultipleListItemsSuccess()
{
alert('Items Updated');
}
function updateMultipleListItemsFailed(sender, args)
{
alert('Request failed. ' + args.get_message() + 'n' + args.get_stackTrace());
}
</script>
Como no nos ha dicho qué API prefiere, a continuación se muestra cómo recuperar y actualizar elementos de la lista utilizando la API JSOM:
function getListItems(listTitle)
{
var deferred = $.Deferred();
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(items);
ctx.executeQueryAsync(function() {
deferred.resolve(items);
},
function(sender,args){
deferred.reject(sender, args);
});
return deferred.promise();
}
function updateListItems(items)
{
var deferred = $.Deferred();
var ctx = items.get_context();
var itemProperties = {'Title': 'John Doe'};
//prepare to update list items
items.get_data().forEach(function(item){
for(var propName in itemProperties) {
item.set_item(propName, itemProperties[propName])
}
item.update();
});
//submit request
ctx.executeQueryAsync(function() {
deferred.resolve(items);
},
function(sender,args){
deferred.reject(sender, args);
});
return deferred.promise();
}
Uso
//update Title for all items in Contacts list
getListItems('Contacts').then(updateListItems).then(logSuccess,logError);
function logError(sender,args){
console.log('An error occured: ' + args.get_message());
}
function logSuccess(sender,args){
console.log('Updated.');
}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)