"The property or field 'Id' has not been initialized. It has not been requested..." when trying to access GUID of Library in SharePoint via JavaScript

25,431

The error:

The property or field 'Id' has not been initialized. It has not been requested…

occurs since List object has not been requested.

Use SP.ClientContext.executeQueryAsync method to execute the current pending request asynchronously on the server

A working example:

var context = SP.ClientContext.get_current();
var web = context.get_web();
var listId =  SP.ListOperation.Selection.getSelectedList(context); 
var list = web.get_lists().getById(listId);
context.load(list, 'Id');  //tell SharePoint to load List Id property
context.executeQueryAsync(  //submit query to the server
  function(){
    console.log("ID:" + list.get_id()); //process result in success callback
  }, 
  function(sender,args){
    console.log(args.get_message());    //handle error in error callback
  }
);
Share:
25,431
Naveen
Author by

Naveen

Updated on May 24, 2020

Comments

  • Naveen
    Naveen almost 4 years

    I am trying to access the ID of Library using client-side object model in SharePoint 2013. But I am getting error as:

    The property or field 'Id' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

    Below is my code:

    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    var items = SP.ListOperation.Selection.getSelectedItems(context);
    var currentLibrary = web.get_lists().getById(SP.ListOperation.Selection.getSelectedList(context));
    context.load(currentLibrary, 'id'); // Tried with 'Id' but still throws error
    console.log(currentLibrary);
    console.log("currentLibrary.get_id = " + currentLibrary.get_id()); // THROWS ERROR!
    

    What am I doing wrong here?

  • Naveen
    Naveen almost 10 years
    I don't think this would return me the GUID of the Library. In my question I have stated that I am trying to access the ID of Library but your answer would return me IDs of items in Library.
  • Si8
    Si8 almost 8 years
    I am doing the same except, I am taking that returned value and querying another list. Please help.
  • Shane Gib.
    Shane Gib. over 7 years
    I should also add that my code looks very similar to Vadim G's example which is why I decided to not include it all, his is well laid out.