繁体   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