Omar El-Anis – 365 Developer

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

Uncategorised

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.

<br>
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.load(collListItem);<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>
    }<br>
}</p>
<p>function onQueryFailed(sender, args) {</p>
<p>    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());<br>
}</p>
<p>

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments