Andrew Andrew - 3 months ago 107
reST (reStructuredText) Question

Angularjs Sharepoint Rest API to showing all columns

I have created a very basic controller to pull data from a SharePoint list, but it only pulls the base Title and created column. It doesn't pull any columns I have created, which are the ones I require. Is there a setting in SP itself to state it is available for the API?

I have used the code off this site:
http://www.c-sharpcorner.com/article/retrieve-sharepoint-list-items-using-angularjs-with-single-a/

As shown here:



var appVar = angular.module('listApp', []);

appVar.controller("controller1", function($scope){
GetListItems($scope, "List1");
});

function GetListItems($scope, listName){
$.ajax({
url: "https://example.com/sites/mysite/_api/web/lists/GetByTitle('"+listName+"')/items",
method: "GET",
async: false,
headers: { "Accept": "application/json;odata=verbose" },
success: function(data){
$scope.items = data.d.results;
},
error: function(sender,args){
console.log(args.get_message());
}
});
}

<div ng-app="listApp">
<div id="App1" ng-controller="controller1">
<h1>First List Items</h1>
<div ng-repeat="item in items">
<p>{{item.Title}}</p>
</div>
</div>
</div>




Answer

SP.ListItemCollection resource endpoint

/_api/web/lists/getByTitle('<list title>')/items

returns all the field values for a list items with some limitation for User/Lookup fields (for performance reasons):

In case of User/Lookup fields only lookup ids will be retrieved by default.

In your example only SP.ListItem.Title property is printed, if you want to display all the returned list item values it could be accomplished like this:

<div ng-repeat="item in items">    
     <p>{{item.Title}}</p>
     <p ng-repeat="(key, value) in item">
          {{ value }} 
     </p>    
</div>

Some another suggestions:

Example

<div ng-app="spApp" ng-controller="TasksController" >
        <table>
        <tr ng-repeat="t in tasks">
            <td ng-repeat="(key, value) in t" ng-if="!value.hasOwnProperty('__deferred') && !value.hasOwnProperty('__metadata')">
                   {{ value }}
             </td>
        </tr>
        </table>
</div>

Controller

angular.module('spApp',[])

.controller('TasksController', function($scope,$http) {
    $scope.tasks = [];

    $scope.getListItems = function(listTitle) {
        return $http({
               method : 'GET',
               url : _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items", 
               headers: { 'Accept':'application/json;odata=verbose' }
        });
      }

     $scope.getListItems("Tasks")
           .success(function(data, status) {
               $scope.tasks = data.d.results;
           })
           .error(function(data, status) {
               console.log("An error occured");
     });
});