"尝试通过JavaScript访问SharePoint中的库的GUID时,属性或字段'Id'尚未初始化.尚未请求..."

Nav*_*een 6 javascript sharepoint sharepoint-clientobject sharepoint-2013

我试图在SharePoint 2013中使用客户端对象模型访问库的ID.但我收到错误:

属性或字段"Id"尚未初始化.尚未请求或请求尚未执行.可能需要明确请求.

以下是我的代码:

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!
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

Vad*_*hev 6

错误:

属性或字段"Id"尚未初始化.它没有被要求......

因为尚未请求List对象发生.

使用SP.ClientContext.executeQueryAsync方法,以在服务器上异步执行当前挂起的请求

一个工作的例子:

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
  }
);
Run Code Online (Sandbox Code Playgroud)