Share data acquisition in Sharepoint 2010
I just needed to do a portioned loading of data onto a page without a PostBack request, and the data should have been taken from the Sharepoint list. Turning to the great Google, I came to the conclusion that I have only one option: REST Interface.
The Client Object Model was not considered due to the bulkiness of the resulting code.
To bind the received data, it was decided to use Angular JS
The service is available immediately, as they say, out of the box, at http: // {siteName} /_vti_bin/ListData.svc. It runs on the oData protocol (an open web protocol for requesting and updating data, which uses HTTP commands as requests and returns responses in Atom, JSON or XML formats).
Those. the task was as follows: “When loading the page, display n entries from the list that meet certain conditions. When you click on the “Get More” button, n more items are loaded. If the elements run out, display a message about the end of the data tape. "
To get information about lists and data types, you can use the request http: // {siteName} /_vti_bin/ListData.svc/$metadata/
We write an html framework
Javascript
Let's consider the line in more detail
In this case, we get from the json service containing 2 fields CustomNumber and CustomTest (in fact there are 3 of them, the service also adds the __metadata attribute which contains the url, 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, we add, for example, & $ filter = CustomNumber gt 50.
Note , if the parameters $ skip or $ top are not used with the filter, then the line
must be replaced by
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 minus can be called only the last paragraph.
In general, I was satisfied with the first impression of REST in Sharepoint.
The Client Object Model was not considered due to the bulkiness of the resulting code.
To bind the received 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 runs on the oData protocol (an open web protocol for requesting and updating data, which uses HTTP commands as requests and returns responses in Atom, JSON or XML formats).
Let's get started
Those. the task was as follows: “When loading the page, display n entries from the list that meet certain conditions. When you click on the “Get More” button, n more items are loaded. If the elements run out, display a message about the end of the data tape. "
To get information about lists and data types, you can use the request http: // {siteName} /_vti_bin/ListData.svc/$metadata/
We write an html framework
CustomTest: {{item.CustomTest}}
CustomNumber: {{item.CustomNumber}}
{{JsonItems.error}}Loading data... Get more...
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;
});
Let's consider the line in more detail
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 containing 2 fields CustomNumber and CustomTest (in fact there are 3 of them, the service also adds the __metadata attribute which contains the url, 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, we add, for example, & $ filter = CustomNumber gt 50.
Note , if the parameters $ skip or $ top are not used with the filter, then the line
var items = data.d;
must be replaced by
var items = data.d.results;
Total
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:
- Case sensitivity in the name of lists, fields, service words
- Different return structure for different queries (in some cases, {"d": {"results": ...}} is returned, in others {"d": {...}})
- Returns in json Title, instead of InternalName (which is very inconvenient when working with Russian names)
Although the minus can be called only the last paragraph.
In general, I was satisfied with the first impression of REST in Sharepoint.