在 Sharepoint 2013 中使用 JavaScript 检索查找的链接列表

Retrieve the linked list of a lookup with JavaScript in Sharepoint 2013

我有两个列表:产品和供应商。
在产品中,我有 3 列:

-产品名称(单行文本)

-供应商(查找供应商:供应商名称)

-价格(选择列表)

在供应商中,我也有 3 列:

-供应商名称(单行文本)

-产品(产品查找:产品名称)

-类别(选择列表)

实际上,在链接到产品(列表)的 .js 中,我正在使用此代码获取供应商列表的信息

var oList = clientContext.get_web().get_lists().getByTitle('Suppliers');

但它是硬编码的,我必须动态化代码,这样我才能将此代码应用到其他列表。

例如:

var NameOfList = "code to get the name of the list which is linked by the lookup (in my case it's the list Suppliers)";
var ColumnsOfList = "NameOfList.getAllColumns (in my case it's Name of Supplier, Product, Category)";
var oList = clientContext.get_web().get_lists().getByTitle(NameOfList);  

var txt = [];
txt.push('The list ' + NameOfList + ' has those columns : ');
for(i=0 ; i<ColumnsOfList.length ; i++){
  txt.push(ColumnsOfList[i]);
}
alert(txt);

会显示The list Suppliers has those columns : Name of Supplier, Product, Category

所以,我想知道使用哪个代码或函数来检索通过查找链接的列表的列表名和列。就我而言,我想将 "Suppliers" 作为列表名,将 "Name of Supplier, Product, Category" 作为列名。

有人可以帮我吗?

编辑: enter image description here

您可以使用 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());
      }
 );