您可以通过使用SPList.get_fields().getByInternalNameOrTitle() 从字段集合中检索查找列的详细信息,调用executeQueryAsync(),然后检查查找列的架构XML(通过SPField.get_schemaXml() 方法)。
从列的架构 XML 中,您可以获取查找列的源列表并运行另一个 executeQueryAsync() 以加载其字段集合,以便获取其所有字段的名称。
这是您的代码可能出现的示例。
var listName = "Products";
var lookupColumn = "Supplier";
var clientContext = new SP.ClientContext();
var list = clientContext.get_web().get_lists().getByTitle(listName);
// get a reference to the lookup field on the current list
var lookupField = list.get_fields().getByInternalNameOrTitle(lookupColumn);
// queue up the lookup field for retrieval
clientContext.load(lookupField);
clientContext.executeQueryAsync(
function(){
// get the lookup list GUID from the lookup column's schema XML:
var lookupListId = lookupField.get_schemaXml().match(/List=(.*?)(?!\S)/g)[0].match(/[^List="][^"]*/)[0];
// get references to the lookup list and its field collection
var lookupList = clientContext.get_web().get_lists().getById(lookupListId);
var lookupListFields = lookupList.get_fields();
// queue up the lookup list and field collection for retrieval
clientContext.load(lookupList);
clientContext.load(lookupListFields);
clientContext.executeQueryAsync(
function(){
var lookupListName = lookupList.get_title();
var fieldNames = [];
// enumerate through the field collection to get the field names
var fieldEnum = lookupListFields.getEnumerator();
while(fieldEnum.moveNext()){
var field = fieldEnum.get_current();
fieldNames.push(field.get_title());
}
doSomethingWithListAndFieldNames(lookupListName,fieldNames);
},
function(sender,args){alert(args.get_message());}
);
},
function(sender,args){ // onError
alert(args.get_message());
}
);
用你自己的函数替换doSomethingWithListAndFieldNames()。
仅从默认视图中获取字段:
如果您只想要在查找列表的默认视图中显示的字段,则需要做一些额外的工作来查询查找列表的视图并查找默认视图,然后从中获取视图字段查看。
var listName = "Products"; // original list title
var lookupColumn = "Supplier"; // lookup column name
var clientContext = new SP.ClientContext();
var list = clientContext.get_web().get_lists().getByTitle(listName);
// get a reference to the lookup field on the current list
var lookupField = list.get_fields().getByInternalNameOrTitle(lookupColumn);
// queue up the lookup field for retrieval
clientContext.load(lookupField);
clientContext.executeQueryAsync(
function(){
// get the lookup list GUID from the lookup column's schema XML:
var lookupListId = lookupField.get_schemaXml().match(/List=(.*?)(?!\S)/g)[0].match(/[^List="][^"]*/)[0];
// get reference to the lookup list
var lookupList = clientContext.get_web().get_lists().getById(lookupListId);
// queue up the lookup list for retrieval
clientContext.load(lookupList);
clientContext.executeQueryAsync(
function(){
var lookupListName = lookupList.get_title();
// get the views on the list
var views = lookupList.get_views();
// queue up the viewsfor retrieval
clientContext.load(views);
clientContext.executeQueryAsync(
function(){
// loop through the views until you find the default view
var viewEnum = views.getEnumerator();
while(viewEnum.moveNext()){
var view = viewEnum.get_current();
if(view.get_defaultView()){
// retrieve the fields from the view
var lookupListFields = view.get_viewFields();
clientContext.load(lookupListFields);
clientContext.executeQueryAsync(
function(){
var fieldNames = [];
// enumerate through the field collection to get the field names
var fieldEnum = lookupListFields.getEnumerator();
while(fieldEnum.moveNext()){
fieldNames.push(fieldEnum.get_current());
}
doSomethingWithListAndFieldNames(lookupListName,fieldNames);
},
function(sender,args){alert(args.get_message());}
);
break;
}
}
},
function(sender,args){alert(args.get_message());});
},
function(sender,args){alert(args.get_message());}
);
},
function(sender,args){ // onError
alert(args.get_message());
}
);