javascript 共享点休息查找值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25234154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:13:27  来源:igfitidea点击:

sharepoint rest lookup value

javascriptrestsharepointsharepoint-2013

提问by mrcandleguy

i'm having a minor issue. I'm using REST/JS to populate a dropdown box, which requires first getting the lookup of a column in a list.

我有一个小问题。我正在使用 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);
            }

        }
    });
}

but the parent value is coming back as "undefined" and i've triple checked that this is the correct list. I've also tried .get_LookupValue() but it comes back as can't get it from an undefined field. So how do you get the value of a lookup field via rest?

但是父值返回为“未定义”,我已经三重检查了这是正确的列表。我也试过 .get_LookupValue() 但它回来了,因为无法从未定义的字段中获取它。那么如何通过rest获取查找字段的值呢?

EDIT:

编辑:

I have done select/expand in the REST Query but have come up with a 400 bad request error:

我已经在 REST 查询中完成了选择/扩展,但出现了 400 个错误的请求错误:

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

Where AT_ARMATECproducts is the lookup field in the ARMATEC Product Models List. AT_ARMATECproducts is a lookup to the ARMATEC Products list which grabs the titles.

其中 AT_ARMATECproducts 是 ARMATEC 产品型号列表中的查找字段。AT_ARMATECproducts 是对获取标题的 ARMATEC 产品列表的查找。

So, it turns out the lookup list was under a different name for some reason, it the list it shows up as AT_ARMATECproducts but seems to actually be AT_Products1.

因此,由于某种原因,查找列表的名称不同,它显示为 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;

now returns [object object], when I tried .Titles it returned with the Titles of the List not the lookup list. And when I tried .AT_Products1.Title it returned with undefined.

现在返回 [object object],当我尝试 .Titles 时,它返回列表的标题而不是查找列表。当我尝试 .AT_Products1.Title 时,它​​返回未定义。

回答by Vadim Gremyachev

When working with User/Lookup field values via SharePoint REST API it is important to differentiate between single-valued and multi-valued lookup fields.

通过 SharePoint REST API 处理用户/查找字段值时,区分单值查找字段和多值查找字段非常重要。

Single-valued lookup fields

单值查找字段

Assume Employeelist with a single-valued lookup field Department

假设Employee列表具有单值查找字段Department

Then the query: /_api/web/lists/getbytitle('<list title>')/itemsreturns lookup Id, for example:

然后查询:/_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);     
  });

The query:

查询:

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

returns projected Department object as shown below:

返回投影的 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);     
  });

Multi-valued lookup fields

多值查找字段

Assume Employeelist with a multi-valued lookup field Departments

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

Then the query: /_api/web/lists/getbytitle('<list title>')/itemsreturns an array of lookup ids, for example:

然后查询:/_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);     
  });

The query:

查询:

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

returns projected Departments array of objects as shown below:

返回投影的 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 read operation function:

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);
        }
    });
}

回答by Yus

Try

尝试

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

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

as you endpoint

作为你的终点

The expand query should only contain the title of the lookup column, and should not include the lookup field. Your select is fine, but the expand isn't.

展开查询应仅包含查阅列的标题,不应包含查阅字段。您的选择很好,但扩展不是。

回答by Iman

below query finally worked for me

下面的查询终于对我有用

  • Mergedis a calculated field in Catslist
  • Catis a lookup field(which looks above Merged column) in the MainFlatlist

            "?$select=Title,number,Cat/Merged&$expand=Cat" + 
            "&$filter=Cat/Merged eq '"+Merged+"'  "
    
  • MergedCats列表中的计算字段
  • CatMainFlat列表中 的查找字段(位于合并列上方)

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

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

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