“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

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?

Answers:

Answer

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
  }
);
Answer

my issue happened to be a silly one, the column I was returning was originally created with the name Requestor_DisplayName, and later changed to Employee_DisplayName so when using:

oListItem.get_item('Employee_DisplayName');

I got the >

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

The issue had nothing to do with the SP.ClientContext.executeQueryAsync method itself...

When I changed the code to:

oListItem.get_item('Requestor_DisplayName');

It ran with out issue. You can use SP CAML Query Helper Online to inspect your your list and columns (as well as build CAML Queries) this is how I discovered my issue:

Screenshot

Hope this helps someone in the future!

Thanks. SG.

Well back again editing this answer as today made another discovery about this error message similar in concept, I did not realize SharePoint will trim your column names after 32 Characters in length...

I got the exact same error message as before in the Developers Tool > debug console (IE f12) but about a different column of course.

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

I was left scratching my head after checking column names in list settings as I had in my JSOM, the column name was "Operations_Approver1_Display_Name" (Yes I was once a COBOL developer so I like long and Meaningful Names LOL)

oListItem.get_item('Operations_Approver1_Display_Name');

All seemed to check out, I thought "Well maybe I have a type in original column name and don't remeber fixing it" So of course I naturally opened up, SP CAML Query Helper Online (man I lobe this tool, yes the b was on purpose LOL).

This is how I discovered that SharePoint has a limit of 32 Characters for column names, just wanted to update this answer since it is highly ranked on search. As you can see in the screenshot below that the InternalName name of the column has been cut short by one character from its "Title" column name (Leave it to me to make this Name 33 characters long just 1 over the limit)

enter image description here

Answer

using Vadim answer:

var oItem ='';
function retrieveWebSite() {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
        var clientContext = new SP.ClientContext.get_current();
        this.oWebsite = clientContext.get_web();
        clientContext.load(this.oWebsite);

        var lstObject = oWebsite.get_lists().getByTitle('Listname');
        oItem = lstObject.getItemById(5);
        clientContext.load(oItem);

        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded), 
            Function.createDelegate(this, this.onQueryFailed)       
        );
    });
}

function onQuerySucceeded(sender, args) {
    var look  = oItem.get_item('LookupColumnName').get_lookupValue();
    var title = oItem.get_item('Title');
    var id = oItem.get_id();
    alert("Loook up column value: "+look);
    alert("Title column: "+title);
    alert("Id column value: "+id);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}
Answer

you are looking for ids then do like this:-

var context = SP.ClientContext.get_current();
var web = context.get_web();
var items = SP.ListOperation.Selection.getSelectedItems(context);
for (var i = 0; i < items.length; i++) {
     var id= items[i].id; 
}

Thanks :)

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.