【问题标题】:Retrieve the linked list of a lookup with JavaScript in Sharepoint 2013在 Sharepoint 2013 中使用 JavaScript 检索查找的链接列表
【发布时间】:2017-04-19 08:45:32
【问题描述】:

我有两个列表:产品和供应商。
在产品中,我有 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

所以,我想知道使用哪个代码或函数来检索通过查找链接的列表的列表名称和列。就我而言,我想将“供应商”作为列表名称,将“供应商名称、产品、类别”作为列名。

有人可以帮帮我吗?

编辑: enter image description here

【问题讨论】:

  • 与 JSOM 方法相比,使用 REST 可以轻松实现此要求。
  • 我是初学者,所以我不太了解 REST 之类的东西:/
  • 要使其动态化,创建自定义配置列表,将列表名称存储在配置列表中。从那里读取并将其传递给您的 javascript 代码。
  • @BatBatsukh 根据您所写的问题,我不确定我是否理解您要完成的工作。您只想在查找列表中显示列的名称吗?您能否进一步详细说明您的问题?

标签: javascript list sharepoint lookup


【解决方案1】:

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

【讨论】:

  • 代码有效,但给了我太多字段,我如何过滤它以仅获取我需要的列名?
  • @BatBatsukh 如果您提前知道需要哪些字段,您可以使用所需字段对其进行硬编码。否则,您可以识别出您想要的内置列,并在枚举查找列表字段时明确排除这些字段添加到fieldNames 数组中。跨度>
  • 是否有功能或解决方案来选择列表中显示的列?例如,在我的供应商列表中,我显示 3 列,供应商名称、产品、类别,是否可以在 fieldNames 中仅保留这 3 个字段或让我对此进行硬编码?因为当我不应用过滤器时,我有很多字段,比如 30fields。
  • 哦,是的,你可以从列表对象的默认视图中获取视图字段。我已经用这样做的代码更新了我的答案。
  • 效果很好!但是你知道为什么不显示 "Title" 而是 "LinkTitle" 吗?我编辑我的帖子并添加屏幕
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多