2017-04-19 70 views
1

我有兩個列表:產品和供應商。
在產品,我有3列:在Sharepoint 2013中檢索使用JavaScript查找的鏈接列表

雜牌產品(單文本行)

-Supplier(供應商查詢:供應商的名稱)

雪牌(選擇列表)

在供應商,我有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

+0

與JSOM方法相比,使用REST可以輕鬆實現此需求。 – Vaibhav

+0

我是一個初學者,所以我不知道很多像REST這樣的東西:/ –

+0

爲了使它動態化,創建自定義配置列表,將列表名稱存儲在配置列表中。從那裏閱讀並傳遞給你的JavaScript代碼。 – Vaibhav

回答

0

您可以通過使用SPList.get_fields().getByInternalNameOrTitle()現場收集獲取它,調用executeQueryAsync(),然後(通過SPField.get_schemaXml()方法)檢查​​查找列的模式XML得到查找列的細節。

從列的模式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()); 
     } 
); 
+0

我會嘗試將其應用於我的代碼。非常感謝 –

+0

代碼的工作原理,但是這給了我太多的字段,我如何過濾它只獲得我需要的列名? –

+0

@BatBatsukh如果您知道提前需要哪些字段,您可以使用所需字段進行硬編碼。否則,您可以識別您*不需要的內置列,並在枚舉查找列表字段時明確排除將這些字段添加到fieldNames數組中。 – Thriggle

相關問題