针对2个列表的SharePoint REST查询

时间:2018-02-22 12:44:18

标签: javascript rest sharepoint-online

我正在尝试同时从2个列表中获取项目('Инструкции'和''test'),然后将它们放在SharePoint页面上的一个表中。

现在我有一个列表的工作解决方案 - 'Инструкции'(见下面的例子)。 我正在使用SharePoint 365。 我不是开发人员。

所以,如果你能用代码回复(不只是说“你需要做这个和这个”)。  感谢。

我尝试应用本文中的代码,但它没有给出预期的结果。 REST query with two lists

$(document).ready(function() {  
    loadMyItems();  
});  

function loadMyItems() {  
        var oDataUrl = "https://site.example/_api/Web/Lists/GetByTitle('Инструкции')/items?$select=Title,EncodedAbsUrl,Device,Related_x0020_application,Key_x0020_words&$filter=(ShowOnSearchPage eq 'Yes')&$top=5000";  
    $.ajax({  
        url: oDataUrl,  
        type: "GET",  
        dataType: "json",  
        headers: {  
            "accept": "application/json;odata=verbose"  
        },  
        success: mySuccHandler,  
        error: myErrHandler  
    });  
}  

function mySuccHandler(data) {  
    try {  
        var dataTableExample = $('#table_id').DataTable();  
        if (dataTableExample != 'undefined') {  
            dataTableExample.destroy();  
        }  
        dataTableExample = $('#table_id').DataTable({  
            scrollY: true,  
            "aaData": data.d.results,  
            "aoColumns": [{  
                "mData": "Title",
                         "render": function(data, type, row, meta){

             return data = '<a href="' + row['EncodedAbsUrl'] + '">' + data + '</a>';}

         },{  
                "mData": "Device",
"render": function( data, type, full, meta) {
if(!data){var returnText = "";}
else {var returnText = data.results;}
return returnText;  }           

         },{  
                "mData": "Related_x0020_application",

         },{  
                "mData": "Key_x0020_words",

           }
]  
        });  
    } catch (e) {  
        alert(e.message);  
    }  
}  

function myErrHandler(data, errCode, errMessage) {  
    alert("Error: " + errMessage);  
}

1 个答案:

答案 0 :(得分:0)

我们可以调用两个REST请求来获取两个数据结果,然后使用JavaScript concat方法连接结果。以下代码供您参考:

function execCrossDomainRequest() {

    var call1 = $.ajax({
      url: "/_api/web/lists/getbytitle('Ansatte')/items?$select=Title",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: successHandler,
            error: errorHandler
   });

    var call2 = $.ajax({
            url: "/_api/web/lists/getbytitle('Kunder')/items?$select=Title",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: successHandler,
            error: errorHandler

   });

    function successHandler(a) {
        var jsonObject = JSON.parse(a.body);
        results = jsonObject.d.results;

        if (allResults.length > 0)
            allResults = allResults.concat(results);

        else
            allResults = results;        
    }

    function errorHandler(data, errorCode, errorMessage) {
        console.log("Could not complete cross-domain call: " + errorMessage);        
    };


    // When both Ajax requests were successful
   $.when(call1, call2).done(function(a1, a2){
      console.log(allResults);
    $('#example').DataTable({
        data:allResults ,
        columns: [
                    { data: "Title" }
        ]
    });
   });  
};

参考以下主题:

Merging the results of 2 REST calls in a dataTable

How to merge and bind the results of two different “listdata.svc” calls to a jquery Datatable (datatable.js)