1

I have a list with two columns: Name and ID. I have the ID value what is a unique field. I would like to get the Name value from the same row using javascript.

Thank you for your help!

0

2 Answers 2

3

I suggest you to use SharePointPlus JavaScript library. You can get row values like this using SharePointPlus:

$SP().list("My List").get({
  fields:"Title",
  where:'ID = 30'
}),function getData(row) {
  for (var i=row.length;i--;) console.log(row[i].getAttribute("Name"));
});
1
  • The code may have a ")" at the end, what is not the part of it.
    – Nefri
    Sep 27, 2017 at 12:18
1

You can get the Name of the list item using the item ID. Here is the code below. The code below assumes that the internal name of the column that you wish to fetch is 'Name' -

var itemId = 'yourItemID';   
var listName = 'yourListName';
var targetListItem;

function getName(itemId) {
  var clientContext = new SP.ClientContext(); 
  var targetList = clientContext.get_web().get_lists().getByTitle(listName);
  targetListItem = targetList.getItemById(itemId);
  clientContext.load(targetListItem, 'Name');
  clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {   
   alert('Name is: ' + targetListItem.get_item('Name'));
}

function onQueryFailed(sender, args) {
   alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
3
  • May I ask you which item's ID is the "itemId"?
    – Nefri
    Sep 29, 2017 at 13:37
  • @Nefri "itemId" is the ID that you have as a unique field, as you mentioned in your question. You pass that ID in the function getName(ID), and get an alert of the Name Oct 3, 2017 at 8:01
  • What if I have to get multiple column values? like name, description etc... how to do that? Jan 10, 2018 at 16:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.