- On page load, accessing all the folders from the library and storing them in a dropdown box.
- On button click event, populate the files in a table based on the selected folder from the dropdown.
The following will apply a Folder collection to the Dropdown list.
HTML Snippet:
- <select id="lib-folder" ng-model="libFolders" ng-options=" fldr as fldr.Title for fldr in Folders" >
- <option value="">--Select Folder--</option>
- </select>
fldr.Title This is the retrieved folder name that is rendered as a value to the dynamically generated <option> element to the dropdown.
Note: If you have not used “<option value=””>--Select Folder--</option>” code, the empty option will be dynamically generated when populating the folders to the dropdown.
Script Snippet:
- $http({
- method: "GET", url:_spPageContextInfo.webAbsoluteUrl+"/_api/web/lists/getbytitle('DocumentLibrary')/items?$select=Id,Title ,FileDirRef,FileRef,FSObjType&$filter=FSObjType%20eq%201",
- headers: {"Accept":"application/json;odata=verbose"}
- }
- ).success(function(data, status, headers, config){
- $scope.Folders = data.d.results;
- $scope.libFolders = $scope.Folder[0];
- }).error(function (data, status, headers, config){
- });
REST API URL: fetches the items with the values for the following fields filtered by folder from the “DocumentLibrary” library.
_spPageContextInfo.webAbsoluteUrl+"/_api/web/lists/getbytitle('DocumentLibrary')/items
View Fields:
- $select=Id,Title ,FileDirRef,FileRef,FSObjType

Filter:
- $filter=FSObjType%20eq%201
- $scope.Folders = data.d.results
The ng-options directive uses this array variable and dynamically generates the options.
- $scope.libFolders = $scope.Folders[0]
Populate files based on the selected Folder:
Now we are done with the rendering of the folder list to the dropdown. To invoke the event to request the files for the selected folder, we have a button and the function “btnGetFiles()” as in the following:
<input type="button" value="Get Files" ng-click="btnGetFiles()"/>
HTML Snippet:
- <!-- Populate Files based on selected Folder -->
- <table width="100%" cellpadding="10" cellspacing="2" class="files-table">
- <thead>
- <tr>
- <th>Title</th>
- <th>Author</th>
- </tr>
- </thead>
- <tbody>
- <tr ng-repeat="file in Files">
- <td><a href="{{file.ServerRelativeUrl}}">{{file.Name}}</a></td>
- <td>{{file.Author.Title}}</td>
- </tr>
- </tbody>
- </table>
- $scope.btnGetFiles = function()
- {
- var selectedFolder = $scope.libFolders.FileRef+"/";
- var resturl = _spPageContextInfo.webAbsoluteUrl+"/_api/web/getfolderbyserverrelativeurl('"+selectedFolder+"')/files?$select=Name,Author/Title&$expand=Author/Title";
- $http(
- {
- method: "GET",
- url:resturl,
- headers: {"Accept":"application/json;odata=verbose"}
- }
- ).success(function(data, status, headers, config){
- $scope.Files = data.d.results;
- }).error(function (data, status, headers, config){
- });
- }
REST API URL
$expand: this option has the ability to retrieve the related properties. This is used to retrieve the author name using a single request instead of sending multiple requests.
$expand=Author/Title: file.Author.Title is to be added in HTML to retrieve the expanded value.
The successful result is stored in the scope variable “$scope.Files” and this is used in a table element to dynamically generate the rows because of the ng-repeat directive.
ng-show
If there is no files retrieved for the folder, use the ng-show directive with the condition to show the proper message as in the following:
<p ng-show="!Files.length">There are no files available under this folder.</p>
Here is the full code:
- <style type="text/css">
- .files-table th{ background-color:#ddd; border:2px solid #fff; text-align:left}
- .files-table td{ background-color:#eee; border:2px solid #fff;}
- .web-heading{ padding:2px;}
- </style>
- <script type="text/javascript" src="/siteassets/angular.min.js"></script>
- <div ng-app="spng-App">
- <h2 class="web-heading">Folder and Files</h2>
- <div ng-controller="spng-WebCtrl">
- <select id="lib-folder" ng-model="libFolders" ng-options="fldr as fldr.Title for fldr in Folders" >
- <option value="">--Select Folder--</option>
- </select>
- <input type="button" value="Get Files" ng-click="btnGetFiles()"/>
- <!-- Populate Files based on selected Folder -->
- <table width="100%" cellpadding="10" cellspacing="2" class="files-table">
- <thead>
- <tr>
- <th>Title</th>
- <th>Author</th>
- </tr>
- </thead>
- <tbody>
- <tr ng-repeat="file in Files">
- <td><a href="{{file.ServerRelativeUrl}}">{{file.Name}}</a></td>
- <td>{{file.Author.Title}}</td>
- </tr>
- </tbody>
- </table>
- <p ng-show="!Files.length">There are no files available under this folder.</p>
- </div>
- </div>
- <script type="text/javascript">
- var spApp= angular.module('spng-App', []);
- spApp.controller('spng-WebCtrl', function($scope, $http){
- //Controller load event to append the choices with folder value to the dropdown
- $http({
- method: "GET",
- url:_spPageContextInfo.webAbsoluteUrl+"/_api/web/lists/getbytitle('DocumentSetLibrary')/items?$select=Id,Title,FileLeafRef,FileDirRef,FileRef,FSObjType&$filter=FSObjType%20eq%201",
- headers: {"Accept":"application/json;odata=verbose"}
- }
- ).success(function(data, status, headers, config){
- //Store the folder collections to the Folders scope variable
- $scope.Folders = data.d.results;
- //Set the default value to the dropdown
- $scope.libFolders = $scope.DocumentSets[0];
- }).error(function (data, status, headers, config){
- });
- //Button click event to populate the files in table
- $scope.btnGetFiles = function(){
- //Get the selected folder from the select element
- var selectedFolder = $scope.libFolders.FileRef+"/";
- var resturl = _spPageContextInfo.webAbsoluteUrl+"/_api/web/getfolderbyserverrelativeurl('"+selectedFolder+"')/files?$select=Name,Author/Title&$expand=Author/Title";
- $http({
- method: "GET",
- url:resturl,
- headers: {"Accept":"application/json;odata=verbose"}
- }
- ).success(function(data, status, headers, config){
- //Store the filecollection for the folder to the Files scope variable
- $scope.Files = data.d.results;
- }).error(function (data, status, headers, config){
- });
- }
- });
- </script>

Still we have many options available in Angular JS and we will continue our exploration in this area. We will welcome the new technologies to ease the coding. Happy learning.

Dinesh GabhanePosted Nov 18, 2019, 3:56 AM
Very Helpful
Gowtham RajamanickamPosted Apr 11, 2015, 9:15 AM
Good one....and share me the tutorial site about angular is with rest
Prasham SabadraPosted Apr 11, 2015, 8:02 AM
Thanks a lot. Actually I was also writing for getting list data and display using Angular but I was struggling for asynchronous call and showing result.
Santhakumar MunuswamyPosted Apr 10, 2015, 2:58 PM
good
Karthik Muthu KaruppanPosted Apr 10, 2015, 2:07 PM
Thanks for sharing