Get SharePoint list item by id using jsom (JavaScript object model)

This SharePoint tutorial explains, how we can retrieve list item by id using javascript object model (jsom) in SharePoint Online/2016/2013. By using SP.List.getItemById Method, we can get list item by id in JavaScript in SharePoint.

We can retrieve list item by item id using jsom (javascript object model) SharePoint online, Below is the JavaScript method which I wrote to retrieve the details by id.

You can use it inside a web part page in a script editor web part or you can use inside any SharePoint hosted add-in.

SP.List.getItemById Method (sp.js) to get item by Id in SharePoint

In sp.js, we can use SP.List.getItemById Method to retrieve list item by Id in SharePoint Online or SharePoint 2013/2016.

Syntax: SP.List.getItemById();

var listItem = list.getItemById(5);

Get list item by id in SharePoint Online/2016/2013 using javascript object model (jsom)

Below is the jsom code to get list item by id using jsom in SharePoint Online/2016/2013

function getListItemDetails(id) {
var ctx = new SP.ClientContext.get_current();
this.website = ctx.get_web();
this.listCollection = website.get_lists();
this.oList = listCollection.getByTitle('MyTestList');
this.oItem = oList.getItemById(id);
ctx.load(oItem);
ctx.load(oItem, 'Include(Id)');
ctx.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFail));
}
function onSuccess(sender, args) {
alert(" ID: " + oItem.get_id());
}
function onFail(sender, args) {
alert('failed to get list. Error:' + args.get_message());
}

Read some SharePoint jsom examples:

I hope this will be helpful to get a list item by id using jsom (JavaScript object model) in SharePoint Online/2016/2013.

>