To return or get items from the list you have two options if you want to retrieve the single item you can use the GetItemById() method or for multiple items use GetItems(CamlQuery).
Replace the siteUrl with your siteUrl for example ("xyz.sharepoint.com") and TitleOfList with your title list from which you want to get the items.
Code to get the list item using the ID.
function getListiem(){
var context = new SP.ClientContext(siteUrl);
var list = context.get_web().get_lists().getByTitle('TitleOfList');
var targetListItem = list.getItemById(listItemID);
context.load(targetListItem);
context.executeQueryAsync(onGettingRecord, onFailedCallback);
}
function onGettingRecord(){
alert(targetListItem.get_item('Title'));
}
function onFailedCallback(sender,args){
alert('Request failed. ' + args.get_message() + '\n'+args.get_stackTrace());
}
Code to get multiple items from the list.
function getItems(){
var context = new SP.ClientContext(siteUrl);
var list = context.get_web().get_lists().getByTitle('TitleOfList');
var caml = new SP.CamlQuery();
returnedItems = list.getItems(caml);
context.load(returnedItems);
context.executeQueryAsync(onGettingItems,onFailedCallback);
}
function onGettingItems(){
var listEnum = returnedItems.getEnumerator();
while(listEnum.moveNext()){
var oListItem = listEnum.get_current();
var itemAmount = oListItem.get_item('Amount');
}
}
function onFailedCallback(sender,args){
alert('Request failed. ' + args.get_message() + '\n'+args.get_stackTrace());
}
Comments
Post a Comment