Sang Tran
Sang Tran

Reputation: 47

Get list items rest call with caml query

Function getlistitems

getlistitembyview

enter image description here

I have 3 pictures. I tried to get list items by caml query but it show error box. function getListItems work well to return caml query but function GetListItemsByView does not work. This is sharepoint get list item should work in different site collection. Someone help please!!!!!!

Upvotes: 0

Views: 658

Answers (1)

LZ_MSFT
LZ_MSFT

Reputation: 4208

Modify the GetListitemsByView function as below.

function GetListitemsByView(webAppUrl,LibraryName,Text_CAML_Query){
    var viewXml = '<View><Query>' + Text_CAML_Query + '</Query></View>';
    var queryPayload = {  
        'query' : {
            '__metadata': { 'type': 'SP.CamlQuery' }, 
            'ViewXml' : viewXml  
        }
    };
    $.ajax({
        url: webAppUrl + "/_api/web/lists/getbytitle('" + LibraryName + "')/getitems",
        type: "POST",
        data: JSON.stringify(queryPayload),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "Content-Type": "application/json; odata=verbose"
        },
        success: function (data) {
            $.each(data.d.results,function(i,item){
                $("#a").append("<li><div>"+item.Title+"</div></li>");
            });
        },
        error: function (data) {         
            alert(JSON.stringify(data));
        }
    });
}

Upvotes: 1

Related Questions