0
function loadDataTable(agentInfo) {

    $.ajax({
        url: vUrl + "/_api/Web/Lists/GetByTitle('TrainingTrackers')/Items?$filter=AVP eq '" + agentInfo + "' or MANAGER eq '" + agentInfo + "'",
        type: "GET",
        dataType: "json",                                                                                                                               
        headers: {
            "accept": "application/json;odata=verbose"
        },
        success: mySuccHandler2,
        error: myErrHandler
    });
}

enter image description here

On the above code, it was getting all data from "AllItems" which is in red circle on the above image but I need to get data from other list view like "CreatedToday" view with circle black on the image.

1
  • You get all items from REST irrespective of the view. Commented Oct 18, 2018 at 20:55

1 Answer 1

0
  • You should do the changes in the REST URL such as sort, filter as you did in the "CreatedToday" view and it will return data accordingly.
  • The data that is returned by REST always depends on the REST URL, so modify it accordingly based on your requirement.

Update:

The following example demonstrates how to retrieve list items for a View using SharePoint REST:

function getListItems(webUrl,listTitle, queryText) 
{
    var viewXml = '<View><Query>' + queryText + '</Query></View>';
    var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems"; 
    var queryPayload = {  
               'query' : {
                      '__metadata': { 'type': 'SP.CamlQuery' }, 
                      'ViewXml' : viewXml  
               }
    };
    return executeJson(url,"POST",null,queryPayload);
}


function getListViewItems(webUrl,listTitle,viewTitle)
{
     var url = webUrl + "/_api/web/lists/getByTitle('" + listTitle + "')/Views/getbytitle('" + viewTitle + "')/ViewQuery";
     return executeJson(url).then(
         function(data){         
             var viewQuery = data.d.ViewQuery;
             return getListItems(webUrl,listTitle,viewQuery); 
         });
}

where:

function executeJson(url,method,headers,payload) 
{
    method = method || 'GET';
    headers = headers || {};
    headers["Accept"] = "application/json;odata=verbose";
    if(method == "POST") {
        headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
    }      
    var ajaxOptions = 
    {       
       url: url,   
       type: method,  
       contentType: "application/json;odata=verbose",
       headers: headers
    };
    if (typeof payload != 'undefined') {
      ajaxOptions.data = JSON.stringify(payload);
    }  
    return $.ajax(ajaxOptions);
}

Usage:

getListViewItems(_spPageContextInfo.webAbsoluteUrl,'Tasks','All Tasks')
.done(function(data)
{
     var items = data.d.results;
     for(var i = 0; i < items.length;i++) {
         console.log(items[i].Title);
     }    
})
.fail(
function(error){
    console.log(JSON.stringify(error));
});

How to get all items in a view using REST API

4
  • Sir, I have here the CustomList which named as TrainingPhonetime then on the TrainingPhonetime list , I have 2 views which are AllItems(default) and CreatedToday. I dont know how to setup to get data from CreatedToday. Commented Oct 19, 2018 at 15:07
  • Im getting the error...responseJSON":{"error":{"code":"-2130575251, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."}}},"status":403,"statusText":"error"} Commented Oct 19, 2018 at 16:03
  • Refer REST API page validation error for SharePoint app Commented Oct 19, 2018 at 16:15
  • Still the same error... Commented Oct 19, 2018 at 18:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.