Get all items from a list without referring to them one by one
-
05-10-2020 - |
Question
I would like to know if there is a way to get all items without referring one by one like I have done in my code? This code returns nothing.
function onS() {
var listString = "";
var Enum = listItems.getEnumerator();
while (Enum.moveNext()) {
var currentItem = Enum.get_current();
var data = currentItem.get_fieldValues();
listString += currentItem.Title;
}
$("#items").html(listString);
}
Solution
if you want to get the JSON response of all field values use get_fieldValues() method
var currentItem = Enum.get_current();
var data = currentItem.get_fieldValues();
it returns the keyvalue pair JSON data like
{'Title':'Title Value',
'ID':'ID value',
'ColumnInternaName':'ColumnValue',
.....
.....}
you can get the title by
data.Title
Id by
data.ID
etc...
if you load items using 'Include' like
context.load(listItems,'Include(Title,ID)')
Then the JSON only contains the data of Title and ID
OTHER TIPS
I am mostly using REST to get all items from a list like the code below:
<script type="text/javascript">
$(document).ready(function() {
// Rest Call
var requestUri = "/_api/lists/getbytitle('News')/items";
$.ajax({
url: requestUri,
type: "GET",
headers: { "ACCEPT": "application/json;odata=verbose" },
success: function (data) {
$.each(data.d.results, function (i, item){
var title = item.Title;
var description = item.Description;
var pictureURLDesc = item.PictureURL.Description;
var pictureURL = item.PictureURL.Url;
var linkDesc = item.Link.Description;
var linkURL = item.Link.Url;
var currentItemID = item.ID;
try
{
// Slice description
if (description.length > 102){
llength = description.slice(0, 102) + " ...";
}
else {
llength = description;
}
}
catch ( err )
{
alert( err );
}
// Check News item
alert("Item Nr: " + currentItemID + "Title: " + title + "Description: " + llength);
})
},
error: function () {
alert("Error getting items");
}
});
});
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange