在Sharepoint 2013中使用JavaScript检索查找的链接列表
我有两份清单:产品和供应商。在Sharepoint 2013中使用JavaScript检索查找的链接列表,javascript,list,sharepoint,lookup,Javascript,List,Sharepoint,Lookup,我有两份清单:产品和供应商。 在产品中,我有3列: -产品名称(单行文字) -供应商(查找供应商:供应商名称) -价格(选择列表) 在“供应商”中,我也有3列: -供应商名称(单行文字) -产品(查找产品:产品名称) -类别(选择列表) 实际上,在链接到Product(list)的.js中,我使用此代码获取列表供应商的信息 var oList = clientContext.get_web().get_lists().getByTitle('Suppliers'); 但是它是硬编码的,我必须对
在产品中,我有3列: -产品名称(单行文字) -供应商(查找供应商:供应商名称) -价格(选择列表) 在“供应商”中,我也有3列: -供应商名称(单行文字) -产品(查找产品:产品名称) -类别(选择列表) 实际上,在链接到Product(list)的.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);
var NameOfList=“获取通过查找链接的列表名称的代码(在我的示例中是列表供应商)”;
var ColumnsOfList=“NameOfList.getAllColumns(在我的例子中是供应商、产品、类别的名称)”;
var oList=clientContext.get_web().get_lists().getByTitle(列表名称);
var txt=[];
txt.push('列表'+NameOfList+'具有以下列:');
对于(i=0;i您可以通过使用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());
}
);
通过使用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());
}
);
与JSOM方法相比,使用REST您可以轻松实现此要求。我是一名初学者,所以我不太了解REST:/要使其动态化,请创建自定义配置列表,将列表名称存储在配置列表中。从那里阅读并将其传递给javascript代码。@BatBatsukh我不确定我是否理解您试图实现的目标基于您的书面问题。您是否只想在查找列表中显示列的名称?您能否在问题中进一步阐述?与JSOM方法相比,使用REST可以轻松实现此要求。我是初学者,所以我不知道REST等很多事情:/n要使其动态化,请创建自定义配置列表,将列表名称存储在nside配置列表。从那里读取它并将其传递给javascript代码。@BatBatsukh我不确定根据所写的问题我是否理解您试图完成的任务。您是否只想在查找列表中显示列的名称?您能否在问题中进一步说明?代码可以工作,但会给我太多的字段,我该如何处理过滤它以仅获取我需要的列名?@BatBatsukh如果您提前知道需要哪些字段,则可以使用所需字段对其进行硬编码。否则,您可以识别不需要的内置列,并在枚举时明确排除将这些字段添加到fieldNames
数组中查找列表字段。是否有功能或解决方案来选择列表中显示的列?例如,在我的供应商列表中,我显示了3列,供应商名称、产品、类别,是否可以仅在字段名称中保留这3个字段
,或者我必须硬编码此字段?因为当我不应用筛选器时,我有很多问题字段,比如30个字段。哦,是的,你可以从列表对象的默认视图中获取视图字段。我已经用代码更新了我的答案。这非常有效!但是你知道为什么不显示“Title”而显示“LinkTitle”吗?我编辑我的帖子并添加屏幕。代码可以工作,但这会给我太多的字段,我如何过滤它以只获得我需要的列名?@BatBatsukh如果您提前知道需要哪些字段,您可以使用所需字段对其进行硬编码。否则,您可以识别不需要的内置列,并明确排除这些字段当您在查找列表字段中枚举时,将字段添加到字段名
数组中。是否有功能或解决方案来选择列表中显示的列?例如,在我的供应商列表中,我显示3列,供应商名称、产品、类别,是否可以仅将这3个字段保留在 fieldNames
或者我必须硬编码吗?因为当我不应用过滤器时,我有很多字段,比如30个字段。哦,是的,你可以从列表对象的默认视图中获取视图字段。我已经用代码更新了我的答案,这样做非常有效!但是你知道为什么不显示“Title”,而是