![](/img/trans.png)
[英]Sharepoint 2013 Add lookup value to list from another list Javascript
[英]Retrieve the linked list of a lookup with JavaScript in Sharepoint 2013
我有兩個清單:產品和供應商。
在產品中,我有3列:
- 產品名稱(單行文字)
- 供應商(供應商查詢:供應商名稱)
- 價格(選擇清單)
在供應商中,我也有3列:
- 供應商名稱(單行文字)
- 產品(產品查詢:產品名稱)
- 類別(選擇清單)
實際上,在鏈接到Product(列表)的.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
。
所以,我想知道用於檢索列表的列表名和列的哪個代碼或函數通過查找鏈接。 在我的情況下,我想將“供應商”作為列表名稱和“供應商名稱,產品,類別”作為列名稱。
有人能幫助我嗎 ?
編輯: 在此處輸入圖像說明
您可以使用SPList.get_fields().getByInternalNameOrTitle()
,調用executeQueryAsync()
,然后檢查查閱列的模式XML(通過SPField.get_schemaXml()
方法)從字段集合中檢索查找列的詳細信息SPList.get_fields().getByInternalNameOrTitle()
。
從列的架構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());
}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.