1

How to get ID from specific item by name like (file.doc)

Code bellow get item by ID :

var itemId = 18; // real id

      var listTitle = "Documents"; // the list title

      var ctx = SP.ClientContext.get_current();
      var spWeb = ctx.get_web();

      var list = ctx.get_web().get_lists().getByTitle(listTitle);

      var item = list.getItemById(itemId);

      ctx.load(spWeb);
      ctx.load(item);

Thank you for help !

1

You need to query the FileLeafRef hidden internal column by forming a CAML query as below.

Do try and modify the below sample code:

var clientContext = SP.ClientContext.get_current();
var list  = clientContext.get_web().get_lists().getByTitle('Documents');
var fileName = "file.doc";  
var collListItem;
var oListItem;

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
                      "<View Scope='RecursiveAll'><Query>" +
                      "<Where>" +
                         "<Eq><FieldRef Name=\"FileLeafRef\"/><Value Type=\"Text\">" + fileName + "</Value></Eq>" +
                      "</Where>" +
                      "</Query></View>");
collListItem = list.getItems(camlQuery);

clientContext.load(collListItem);
clientContext.executeQueryAsync(function(){
    var listItemEnumerator = collListItem.getEnumerator();
    while(listItemEnumerator.moveNext()) {    
        var oListItem = listItemEnumerator.get_current();
        console.log(oListItem.get_id());
    }
},function(sender, args){
    console.log("something went wrong");
    console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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