【问题标题】:sharepoint rest lookup valuesharepoint 剩余查找值
【发布时间】:2015-08-12 14:49:32
【问题描述】:

我遇到了一个小问题。我正在使用 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获取查找字段的值呢?

编辑:

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

    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 时,它返回列表的标题而不是查找列表。当我尝试 .AT_Products1.Title 时,它​​返回未定义。

【问题讨论】:

    标签: javascript rest sharepoint sharepoint-2013


    【解决方案1】:

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

    单值查找字段

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

    然后查询:/_api/web/lists/getbytitle('<list title>')/items返回lookup 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);     
      });
    

    多值查找字段

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

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

    【讨论】:

      【解决方案2】:

      试试

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

      作为你的端点

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

      【讨论】:

        【解决方案3】:

        下面的查询终于为我工作了

        • MergedCats 列表中的计算字段
        • CatMainFlat 列表中的查找字段(在 Merged 列上方)

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

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

        【讨论】:

          猜你喜欢
          • 2013-02-04
          • 1970-01-01
          • 2020-11-27
          • 2017-06-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多