如何比较两个SharePoint列表并返回匹配的列表项

时间:2020-08-30 21:37:07

标签: ajax rest sharepoint datatables sharepoint-online

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

  1. 获取当前用户的ID
  2. 在列表A中,获取“位置”和“位置”字段,其中ID等于当前用户的ID
  3. 在列表B中,从“位置”和“位置”字段中获取所有项目
  4. 比较两个列表中“位置”和“位置”字段中的值,并返回匹配的任何列表项
  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) {
        }
    });
});

1 个答案:

答案 0 :(得分:0)

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

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

外部列表使用过滤器功能遍历过滤器中另一个列表的值并返回满足您要求的值

当然,您也可以直接使用double for循环:

[{"Value":1,"Name":"Child Integer 1","Type":"Integer"},{"Value":"2020-08-31T08:29:11.9002559+05:30","Name":"Child DateTime 1","Type":"DateTime"}]
相关问题