【问题标题】:How to compare two SharePoint lists and return matching list items如何比较两个 SharePoint 列表并返回匹配的列表项
【发布时间】:2020-12-19 01:49:16
【问题描述】:

我有两个结构相同的 SharePoint Online 2016 列表,我想比较这些列表并使用 REST API 返回匹配的字段。目标是单击一个按钮,该按钮将:

  1. 获取当前用户的ID
  2. 在列表 A 中,获取 ID 等于当前用户 ID 的 Position 和 Location 字段
  3. 在列表 B 中,从 Position 和 Location 字段中获取所有项目
  4. 比较两个列表中 Position 和 Location 字段中的值,并返回 任何 个匹配的列表项
  5. 在 jQuery 数据表中显示匹配结果

两个列表中的字段都是名称相同的选择列。

我可以通过 REST 从两个列表中检索数据,但我不确定如何实现比较/匹配步骤。任何见解将不胜感激。我也对其他方法持开放态度。

<input type="button" id="onClick" value="Get Matches"> 
// Get Location and Position fields from List A where user ID equals current user
$(function(){
    $("#onClick").click(function(){
        var userId = _spPageContextInfo.userId; 
        var ListAurl = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('ListA')/items?$select=location/Title,position/Title&$expand=location/Title,position/Title&$top=5000&$filter=(AuthorId eq '" + userId + "')";
        $.ajax({
            url: ListAurl,
            type: "GET",
            headers: {
                 "accept":"application/json; odata=verbose"
            },
            success: onSuccess,
            error: onError
        });
        
        function onSuccess(data) {
            var ListAItems = data.d.results;
        }
        function onError(error) {
        }

// Get Location and Position fields from List B
        var ListBurl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ListB')/items?$select=location/Title,position/Title&$expand=location/Title,position/Title&$top=5000";
        $.ajax({
            url: ListBurl,
            type: "GET",
            headers: {
                "accept":"application/json; odata=verbose"
            },
            success: onSuccess,
            error: onError
        });

        function onSuccess(data) {
            var ListBItems = data.d.results;
        }

        function onError(error) {
        }
    });
});

【问题讨论】:

    标签: ajax rest sharepoint datatables sharepoint-online


    【解决方案1】:

    你可以在js中使用过滤功能。

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

    外层列表使用过滤器函数遍历过滤器中另一个列表的值,返回符合你要求的值

    当然也可以直接使用双for循环:

     for (let i = 0; i < lista.length; i++) {
            for (let j = 0; j < listb.length; j++) {
               //do compare action                 
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2010-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 2023-03-04
      相关资源
      最近更新 更多