簡體   English   中英

sharepoint rest lookup值

[英]sharepoint rest lookup value

我有一個小問題。 我正在使用REST / JS來填充下拉框,這需要首先查找列表中的列。

//Product Model Cascade
document.getElementById("productDD").onchange = function() {
    var prod = this.options[document.getElementById("productDD").selectedIndex].text;
    var select = document.getElementById("productModelDD");
    $(select).empty();


    var call2 = $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists(guid'bb392616-24ee-47e2-9365-f17400348373')/Items", //the list
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }

    });
    call2.done(function(data, textStatus, jqXHR) {
        var select = document.getElementById("productModellDD");
        for (index in data.d.results) {
            var parent = data.d.results[index].AT_ARMATECproducts; //Lookup column 
            console.log("parent var: " + parent);
            if (parent == prod) {
                var op = document.createElement("option");
                op.text = data.d.results[index].Title;
                op.value = data.d.results[index].Title;
                select.appendChild(op);
            }

        }
    });
}

但是父值返回為“未定義”,我已經三次檢查這是正確的列表。 我也試過.get_LookupValue(),但它回來了,因為無法從未定義的字段中獲取它。 那么如何通過休息獲得查找字段的值?

編輯:

我在REST查詢中完成了select / expand,但是出現了400錯誤的請求錯誤:

    url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists(guid'bb392616-24ee-47e2-9365-f17400348373')/items?$select=Title,AT_ARMATECproducts/Title&$expand=AT_ARMATECproducts/Title",

其中AT_ARMATECproducts是ARMATEC產品型號列表中的查找字段。 AT_ARMATECproducts是對ARMATEC產品列表的查找,用於獲取標題。

因此,事實證明,由於某種原因,查找列表的名稱不同,它顯示為AT_ARMATECproducts的列表,但實際上似乎是AT_Products1。

url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists(guid'bb392616-24ee-47e2-9365-f17400348373')/items?$select=Title,AT_Products1/Title&$expand=AT_Products1/Title",
var parent = data.d.results[index].AT_Products1;

現在返回[object object],當我嘗試.Titles返回時List的標題不是查找列表。 當我嘗試.AT_Products1.Title它返回undefined。

通過SharePoint REST API處理User / Lookup字段值時,區分單值和多值查找字段非常重要。

單值查找字段

假設具有單值查找字段Department Employee列表

然后查詢: /_api/web/lists/getbytitle('<list title>')/items返回查找ID,例如:

getListItems(_spPageContextInfo.webAbsoluteUrl,'','Employees',
  function(items){
     if(items.length > 0)
        console.log(items[0].DepartmentId);   //returns LookupId
  },
  function(error){
     console.log(error.responseText);     
  });

查詢:

 /_api/web/lists/getbytitle('Employees')/items?$select=Department/Title,Department/Id&$expand=Department

返回投影的Department對象,如下所示:

getListItems(_spPageContextInfo.webAbsoluteUrl,'?$select=Department/Title,Department/Id&$expand=Department','Employees',
  function(items){
     if(items.length > 0)
        console.log(items[0].Department.Title);   
        console.log(items[0].Department.Id);   
  },
  function(error){
     console.log(error.responseText);     
  });

多值查找字段

假設具有多值查找字段Departments Employee列表

然后查詢: /_api/web/lists/getbytitle('<list title>')/items返回一個查找id數組,例如:

getListItems(_spPageContextInfo.webAbsoluteUrl,'','Employees',
  function(items){
     if(items.length > 0)
        console.log(items[0].DepartmentsId);   //returns an array [LookupId1, LookupId1 .. LookupIdN]
  },
  function(error){
     console.log(error.responseText);     
  });

查詢:

/_api/web/lists/getbytitle('Employees')/items?$select=Departments/Title,Departments/Id&$expand=Departments

返回投影的Departments對象數組,如下所示:

getListItems(_spPageContextInfo.webAbsoluteUrl,'?$select=Departments/Title,Departments/Id&$expand=Departments','Employees',
  function(items){
     if(items.length > 0)
        if(items[0].Departments.results.length > 0) {
           console.log(items[0].Departments.results[0].Title);   
           console.log(items[0].Departments.results[0].Id);   
        }   
  },
  function(error){
     console.log(error.responseText);     
  });

SharePoint 2013 REST讀取操作功能:

function getListItems(url, query, listName, success, failure) {
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listName + "')/items" + query,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            success(data.d.results);
        },
        error: function (data) {
            failure(data);
        }
    });
}

嘗試

/_api/Web/Lists(guid'bb392616-24ee-47e2-9365-f17400348373')/items?$select=Title,AT_ARMATECproducts/Title&$expand=AT_ARMATECproducts"

作為你的終點

展開查詢應僅包含查閱列的標題,並且不應包含查找字段。 您的選擇很好,但擴展不是。

下面的查詢終於為我工作了

  • 合並Cats列表中的計算字段
  • CatMainFlat列表中的查找字段(在Merged列上方查看)

      "?$select=Title,number,Cat/Merged&$expand=Cat" + "&$filter=Cat/Merged eq '"+Merged+"' " 

https://sharepoint.stackexchange.com/a/152997/16880

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM