Skip to content

ShareCoffee.REST.AngularJS

Thorsten Hans edited this page Jan 16, 2014 · 6 revisions

ShareCoffee works fine with Angular's $http in order to access data from SharePoint out of your SharePoint-Hosted App.

HostWeb - AppWeb

ShareCoffee is offering both, interaction with the AppWeb and interactions with the HostWeb. If you pass a HostWebUrl to any for.angularJS() call, ShareCoffee will create a configuration-object which allows HostWeb-Access.

Configuring Requests

Each of the following methods is configured by a single parameter of type ShareCoffee.REST.angularProperties. You can either create a new instance by using the constructor ShareCoffee.REST.angularProperties() or you can pass a JSON object with corresponding properties.

/// Create property-object by using the constructor
var properties = new ShareCoffee.REST.RequestProperties(url, payload, hostWebUrl, eTag);
/// Create a property-object by using JSON syntax
var properties2 = { url: 'web/title')

Reading data using ShareCoffee.REST.build.read.for.angularJS()

In order to create a configured properties object which can easily be passed to AngularJS' $http you just have to call ShareCoffee.REST.build.read.for.angularJS() and pass the REST endpoint you're looking for

Usage

$http(ShareCoffee.REST.build.read.for.angularJS({url:'web/lists?$select=Title,Id'}))
  .success(function (data) {
    $scope.lists = data.d.results;
  })
  .error(function (data, status) {
    console.log(data);
    console.log(status);
  });

Creating elements using ShareCoffee.REST.build.create.for.angularJS()

description: TODO

Usage

var payload = {
  '__metadata': { 'type': 'SP.List' },
  'AllowContentTypes': true,
  'BaseTemplate': 100,
  'ContentTypesEnabled': true,
  'Description': 'My list created by REST',
  'Title': "Thorstens List"
};

$http(ShareCoffee.REST.build.create.for.angularJS({url: "web/lists/", payload: payload}))
  .success(function (data) {
    ShareCoffee.UI.showNotification('List has been created');
    $scope.refreshLists();
    $scope.customListId = data.d.Id;
  })
  .error(function (data,status) {
    console.log(data);
    console.log(status)
  });

Updating elements using ShareCoffee.REST.build.update.for.angularJS()

description TODO

Usage

var payload = {
  '__metadata': { 'type': 'SP.List' },
  'Title': 'CHANGED BY REST - Thorstens list'
};
$http(ShareCoffee.REST.build.update.for.angularJS(
  {url: "web/lists('" + $scope.customListId + "')", payload: payload}))
  .success(function (data) {
    ShareCoffee.UI.showNotification('List has been updated');
    $scope.refreshLists();
  });

Deleting elements using ShareCoffee.REST.build.delete.for.angularJS()

description: TODO

Usage

$http(ShareCoffee.REST.build.delete.for.angularJS({url: "web/lists('" + $scope.customListId + "')"}))
  .success(function (data) {
    ShareCoffee.UI.showNotification('List has been deleted');
    $scope.refreshLists();
  });

Clone this wiki locally