6

I am using SharePoint 2013 REST to show data from my list,

one of my field in announce list is AnnouncedBy which is a user packer field

or is a lookup field

how can I display it or get it using SharePoint 2013 REST?

 var app = angular.module('SmartphoneOSList', ['ngSanitize']);  
 app.controller('ListController', function ($scope, $http) {  
 $http({  
    method: 'GET',  
    url: "https://myserver/_api/web/lists/getByTitle('announce')/items?$top=2&$orderby=Modified desc",  
    headers: { "Accept": "application/json;odata=verbose" }  
   }).success(function (data, status, headers, config) {  
     $scope.items = data.d.results;  

   }).error(function (data, status, headers, config) {  
    //...
    });  
});
1
  • where you able to fix this issue?. My lookup output is in json
    – Valentine
    Commented Jul 30, 2018 at 6:42

2 Answers 2

10

In order to select User or Lookup field values the $expand operator is used (for making a projection of AnnouncedBy column with User Information List).

For example, the following query

https://contoso.sharepoint.com/news/_api/web/lists/getByTitle('Announcements')/items?$select=*,AnnouncedBy/Title&$expand=AnnouncedBy

will return the following result:

enter image description here

Complete example

The example demonstrates how to bind User field value in AngularJs:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular-sanitize.js"></script>
<script> 
var app = angular.module('AnnouncementsList', ['ngSanitize']);  
app.controller('ListController', function ($scope, $http) {  
    $http({  
        method: 'GET',  
        url: "https://contoso.sharepoint.com/_api/web/lists/getByTitle('Announcements')/items?$select=*,AnnouncedBy/Title&$expand=AnnouncedBy",  
        headers: { "Accept": "application/json;odata=verbose" }  
    }).success(function (data, status, headers, config) {  
        $scope.items = data.d.results;  
    }).error(function (data, status, headers, config) {  
        //error handling goes here..
    });  
});  
</script>  


<h1>Announcements</h1>  
<div ng-app="AnnouncementsList">  
    <ul ng-controller="ListController">   
        <li ng-repeat="item in items">  
            <span>{{item.Title}}</span> By <span>{{item.AnnouncedBy.Title}}</span>  
        </li>  
    </ul>  
</div>

Result

enter image description here

3
  • 1
    +1 for AngularJs :) Commented Nov 12, 2014 at 9:07
  • Yes the questioner was still confused, thanks for the details :) Commented Nov 12, 2014 at 9:12
  • @VadimGremyachev But if I expand the field and see the results, lookup field title is coming as null and id is coming as properly.
    – Mihir
    Commented Aug 20, 2015 at 12:18
3

Try adding following in the URL

$select=AnnouncedBy/Id,AnnouncedBy/Title,*&$expand=AnnouncedBy

Reference:

SharePoint 2013–retrieving the lookup value of User fields using the REST API

Getting User Information with the SharePoint 2013 REST API

6
  • I had seen those posts but they are totally not constructive and not working
    – MJ X
    Commented Nov 11, 2014 at 11:01
  • When I add your code: it looks like blow: ABC - 2014-11-11T09:04:14Z EYXdsldj lalsdfljalsjf​ {"__metadata":{"id":"b164b165-425d-418c-9629-e8900d0507f5","type":"SP.Data.UserInfoItem"},"Id":10,"Title":"Shofiqul Islam"} Readmore...
    – MJ X
    Commented Nov 11, 2014 at 11:08
  • Shofiqul Islam this is the user name right ? Commented Nov 11, 2014 at 11:27
  • Yes that is username
    – MJ X
    Commented Nov 11, 2014 at 16:29
  • You can find how to access these in the given links. What else are you looking for? Commented Nov 11, 2014 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.