Omar El-Anis – 365 Developer

My experiences with SharePoint, Flows, PowerApps, JavaScript and all of the other lovely web technologies out there!


Iterate through all items in a SharePoint list using JSOM

The following code can be used to iterate through all of the items within a list and pull out all of the relevant fields from each item.
This is very useful for building your own content using the data stored within a list.
You can for example create some functions in JavaScript to pull out all of the information and dynamically build some HTML code to then insert in to the page.

var clientContext = new SP.ClientContext.get_current();<br>
//change 'Images' .getByTitle('Images') to the name of your list or use getByID instead<br>
var oList = clientContext.get_web().get_lists().getByTitle('Images');<br>
var camlQuery = new SP.CamlQuery();<br>
this.collListItem = oList.getItems(camlQuery);<br>
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));<br>
var oListItem;</p>
<p>function onQuerySucceeded(sender, args) {<br>
    var listItemInfo = '';<br>
    var listItemEnumerator = collListItem.getEnumerator();<br>
    var i = 0;<br>
    var string = "";a<br>
    while (listItemEnumerator.moveNext()) {<br>
        oListItem = listItemEnumerator.get_current();<br>
        var Id = oListItem.get_id();<br>
        //use .get_item('fieldname') to retrieve specific column data<br>
        var Title = oListItem.get_item('Title');<br>
        // add in your code here to use the details pulled out from the current item<br>
        // example shown below<br>
        string = string + "The title of item" + Id + " is " + Title<br>
<p>function onQueryFailed(sender, args) {</p>
<p>    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());<br>

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments