ysfibm
ysfibm

Reputation: 436

How to retrieve list items title using JavaScript

I'm trying to retrieve list items Title to insert it into a div section in my SharePoint home page, I've found this code which retrieve item URL.

it works just fine.

var clientContext = SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle("Artdesk alertes");

var caml = new SP.CamlQuery();
caml.set_viewXml("<View />");

var listItemCollection = list.getItems(caml);

clientContext.load(list, "DefaultDisplayFormUrl");
clientContext.load(listItemCollection);

clientContext.executeQueryAsync(function() {

    for(var i in listItemCollection.get_data()) {

        console.log( "DispFormRelativeUrl: " + list.get_defaultDisplayFormUrl() + "?ID=" +listItemCollection.get_data()[i].get_id() );
    }

}, function(sender, args) {
    window.console && console.log(args.get_message());  
});

How do I get the list item Title using the JavaScript object model?

Upvotes: 1

Views: 9053

Answers (2)

Alex K
Alex K

Reputation: 395

clientContext.load(list, "DefaultDisplayFormUrl");

You are requesting "DefaultDisplayFormUrl" property here. You can specify Title property or just remove requesting specific properties to get every one of them (not recommended - you must request only those you need to reduce load). So it will be clientContext.load(list, "Title") or clientContext.load(list);

clientContext.executeQueryAsync(function() {    
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();       
        console.log("List Title", oListItem.get_item('Title'));
    }
}, <...>);

Note that you can get any property via get_item method (but you must request to load that property or it will throw an exception).

Here is the working code:

var clientContext = SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle("Artdesk alertes");

var caml = new SP.CamlQuery();
caml.set_viewXml(""); // empty query also works

var listItemCollection = list.getItems(caml);

clientContext.load(listItemCollection); // i requested every property

clientContext.executeQueryAsync(function() {
    var listItemEnumerator = listItemCollection.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        console.log(oListItem.get_item('Title'));
    }

}, function(sender, args) {
    window.console && console.log(args.get_message());  
});

Upvotes: 1

jpussacq
jpussacq

Reputation: 577

Check get_item('Title') on this code from MSDN:

function retrieveListItems(siteUrl) {
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Announcements');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(
        '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
        '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' + 
        '<RowLimit>10</RowLimit></View>'
    );
    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    ); 
}

function onQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nID: ' + oListItem.get_id() + 
            '\nTitle: ' + oListItem.get_item('Title') + 
            '\nBody: ' + oListItem.get_item('Body');
    }

    alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}

Upvotes: 0

Related Questions