📜 ⬆️ ⬇️

Batch data retrieval in Sharepoint 2010

I needed something like that once, to do a portion of the data loading on the page without a PostBack request, and the data should have been taken from the Sharepoint list. Turning to the great Google came to the conclusion that I have only one option: REST Interface.
Client Object Model was not considered due to the bulkiness of the resulting code.
For binding data, it was decided to use Angular JS

REST Interface


The service is available immediately, as they say, out of the box, at http: // {siteName} /_vti_bin/ListData.svc. It works via the oData protocol (an open web protocol for querying and updating data, which uses HTTP commands as queries and returns answers in Atom, JSON or XML formats).

Let's get started


Those. The task looked like this: “When the page loads, display n entries from the list that meet certain conditions. When you click on the "Get more" button, n more elements are loaded. If the items have run out, output a message about the end of the data feed. ”
For information about lists and data types, you can use the query http: // {siteName} /_vti_bin/ListData.svc/$metadata/
We write html framework
<SharePoint:ScriptLink ID="ScriptLinkAngular" runat="server" Name="SharePointProject/js/angular/angular.js" Localizable="false"/> <SharePoint:ScriptLink ID="ScriptLinkApp" runat="server" Name="SharePointProject/js/app.js" Localizable="false"/> <div ng-app="testApp"> <div ng-controller="ListCtrl"> <div ng-repeat="item in JsonItems.items" style="margin-top: 10px;"> <pre> CustomTest: <b>{{item.CustomTest}}</b> <br/> CustomNumber: <b>{{item.CustomNumber}}</b></pre> </div> <!--     --> <div ng-show="JsonItems.showError" class="alert alert-danger" >{{JsonItems.error}}</div> <!--    --> <div ng-show="JsonItems.busy" class="alert alert-info" >Loading data...</div> <!--   --> <div ng-hide="JsonItems.busy" class="alert alert-success" ng-click="JsonItems.nextPage()" > Get more... </div> </div> </div> 

Javascript
 var testApp = angular.module('testApp', []); testApp.controller('ListCtrl', ListCtrl); function ListCtrl($scope, $http, Items) { $scope.JsonItems= new Items(); $scope.JsonItems.nextPage(); } testApp.factory('Items', function($http) { var Items = function() { this.items = []; this.busy = false; this.after = 0; this.count=25; this.showError= function () { return this.error.length>0}; this.error=""; }; Items.prototype.nextPage = function() { if (this.busy) return; this.busy = true; var url = "http://{siteName}/_vti_bin/ListData.svc/Test?$skip=" + this.after + "&$top="+this.count +"&$orderby=CustomNumber&$select=CustomNumber,CustomTest"; $http({method: 'GET', url: url }).success(function(data) { var items = data.d; if (items.length > 0){ for (var i = 0; i < items.length; i++) { this.items.push(items[i]); } else { this.error = "No more data" } this.after += this.count; this.busy = false; } }.bind(this)). error(function(data, status) { this.error = "Error: " + data; this.busy = false; }.bind(this)); }; return Items; }); 

')
Consider more string
  var url = "http://{siteName}/_vti_bin/ListData.svc/Test?$skip=" + this.after + "&$top="+this.count +"&$orderby=CustomNumber&$select=CustomNumber,CustomTest"; 

In this case, we get from the json service, which contains 2 CustomNumber and CustomTest fields (in fact, there are 3 of them, the service adds another __metadata attribute which contains the url, the type and etag of the element). The result consists of n elements and is sorted by the CustomNumber column. If you need to add filtering, then append, for example, & $ filter = CustomNumber gt 50.
Note , if the $ skip or $ top parameters are not used with the filter, then the string
 var items = data.d; 

need to be replaced by
 var items = data.d.results; 

Total


The REST Interface has a fairly flexible filtering system and makes it easy to make complex queries.
Of the minuses, I would single out the following:
Although the last point can be called a minus.
In general, I was pleased with the first impression from REST in Sharepoint.

Source: https://habr.com/ru/post/200936/


All Articles