I have a Document Library with multiple documents. One can select multiple documents even all the documents at a time.
I need to fetch the documents name which are selected in Javascript using JQuery & SPServices
First, I am able to get IDs which are selected using the following code

var context = SP.ClientContext.get_current();
var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);

So, I am getting the IDs which are selected, now I need to fetch the name of those documents by the IDs.
Guys can anyone please tell me what is the best way to do so.

  • I'm using SP 2013, Where i need to paste the above code?
    – Netaji J
    Commented Oct 21, 2019 at 7:15

1 Answer 1

var context = SP.ClientContext.get_current();
var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);

var fileName = [];

for (var i = 0; i < selectedItems.length; i++) {
    var itemId = selectedItems[i].id;
    fileName[i] = GetName(itemId);

function GetName(itemId) {        
    var currentList = context.get_web().get_lists().getById(SP.ListOperation.Selection.getSelectedList());
    this.singleItem = currentList.getItemById(itemId);

    context.load(this.singleItem, 'Name');
    context.executeQueryAsync(Function.createDelegate(this, this.OnSucceeded), Function.createDelegate(this, this.OnFailed));

function OnSucceeded() {
    this.NameValue = this.singleItem.get_item('Name');

function OnFailed(sender, args) {
    alert('Error occurred: ' + args.get_message());

Here I have added column 'Name', you can use any column you want as per requirement.

var fileName = []; 

this variable will give you list of all your selected document names

  • you can mark it as answer if works for you. so someone seeking for same will be benefited
    – Gaurravs
    Commented Mar 28, 2016 at 8:59
  • What is this.NameValue inside function OnSucceeded() ? Thanks
    – zed
    Commented Apr 13, 2018 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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