0
votes

I tried to get a list of items from the Sharepoint library by view name. I had ajax rest API URL:

url: webapp + "_api/web/list/getbytitle" + "('" + LibraryName + "')/View/getbytitle" +"('" + viewName + "')"
method:"GET"
header:"Accept":"application/json;odata=verbose

How can I get all the items in view name?

1

1 Answers

0
votes

please refer the following code snippet to get items from specific view, Rest API not provider items endpoint return from a view directly. So please do the following:

  1. perform the first request to get CAML Query for List View using SP.View.viewQuery property
  2. perform the second request to retrieve List Items by specifying CAML Query:

        getListItemsForView(_spPageContextInfo.webAbsoluteUrl,'MyList12','View1')
        .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));
        });
    
    
    
     function getListItemsForView(webUrl,listTitle,viewTitle)
        {
             var viewQueryUrl = webUrl + "/_api/web/lists/getByTitle('" + listTitle + "')/Views/getbytitle('" + viewTitle + "')/ViewQuery";
             return getJson(viewQueryUrl).then(
                 function(data){         
                     var viewQuery = data.d.ViewQuery;
                     return getListItems(webUrl,listTitle,viewQuery); 
                 });
        }
    
        function getJson(url) 
        {
            return $.ajax({       
               url: url,   
               type: "GET",  
               contentType: "application/json;odata=verbose",
               headers: { 
                  "Accept": "application/json;odata=verbose"
               }
            });
        }
    
    
    
     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 $.ajax({
                   url: url,
                   method: "POST",
                   data: JSON.stringify(queryPayload),
                   headers: {
                      "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                      "Accept": "application/json; odata=verbose",
                      "content-type": "application/json; odata=verbose"
                   }
             });
        }
    

Same question has been answered here:

Using REST to fetch SharePoint View Items