Skip to content

Instantly share code, notes, and snippets.

@eirikb

eirikb/app.html Secret

Created May 21, 2015 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eirikb/92db02b9bcf3dfc8b3e2 to your computer and use it in GitHub Desktop.
Save eirikb/92db02b9bcf3dfc8b3e2 to your computer and use it in GitHub Desktop.
<div ng-controller="UserCtrl">
<p>First user: {{firstUser.get_title()}}</p>
<p>First group: {{firstGroup.get_title()}}</p>
</div>
<div ng-controller="ListCtrl">
<p>First list: {{firstList.get_title()}}</p>
<p>First item: {{firstItem.get_item('Title')}}</p>
</div>
app.factory('toArray', function () {
return function (x) {
var e = x.getEnumerator();
var r = [];
while (e.moveNext()) r.push(e.get_current());
return r;
};
});
app.factory('ctx', function ($q) {
var ctx = new SP.ClientContext("/");
ctx.execThen = (function () {
var q;
return function () {
if (!q) {
q = $q.when().then(function () {
return $q(function (resolve, reject) {
ctx.executeQueryAsync(resolve, reject);
}).then(function () {
q = null;
});
});
}
return q;
};
})();
return ctx;
});
app.controller('UserCtrl', function ($scope, ctx, toArray) {
var users = ctx.get_web().get_siteUsers();
ctx.load(users, 'Include(Title)');
ctx.execThen().then(function () {
var firstUser = $scope.firstUser = toArray(users)[0];
var groups = firstUser.get_groups();
ctx.load(groups, 'Include(Title)');
ctx.execThen().then(function () {
$scope.firstGroup = toArray(groups)[0];
}
);
});
});
app.controller('ListCtrl', function ($scope, ctx, toArray) {
var lists = ctx.get_web().get_lists();
ctx.load(lists, 'Include(Title)');
ctx.execThen().then(function () {
var firstList = $scope.firstList = toArray(lists)[0];
var items = firstList.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(items, 'Include(Title)');
ctx.execThen().then(function () {
$scope.firstItem = toArray(items)[0];
}
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment