├── LICENSE ├── README.md └── angular-sharepoint.js /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 MattGibz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | angular-sharepoint 2 | ================== 3 | This project is a framework to assist in building [AngularJS](http://angularjs.org/) SharePoint applications. 4 | You can use it to quickly interact with host web or app web lists. The framework leverages the SharePoint REST API and provides a chainable syntax for querying SharePoint data. 5 | 6 | #Setup 7 | ####Include the module: 8 | ``` 9 | var myApp = angular.module('myApp', ['ngSP']); 10 | ``` 11 | ####Reference the service in your controller: 12 | ``` 13 | myApp.controller('AppCtrl', ['$scope', '$spService', function ($scope, $spService) { 14 | }]); 15 | ``` 16 | 17 | #Examples 18 | 19 | ###Lists 20 | ####Get lists in the host web: 21 | ``` 22 | $spService.lists.value().then( 23 | function (data) { 24 | //do stuff 25 | }, 26 | function (sender, args) { 27 | console.log('error'); 28 | } 29 | ); 30 | ``` 31 | 32 | ####Get lists by [template type](http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx) value: 33 | ``` 34 | $spService.lists.getByBaseTemplateType('104').value().then( 35 | function (data) { 36 | //expect announcement lists 37 | //do stuff 38 | }, 39 | function (sender, args) { 40 | console.log('error'); 41 | } 42 | ); 43 | ``` 44 | 45 | ####Get task lists: 46 | ``` 47 | $spService.lists.getTaskLists().value().then( 48 | function (data) { 49 | //expect task lists 50 | //do stuff 51 | }, 52 | function (sender, args) { 53 | console.log('error'); 54 | } 55 | ); 56 | ``` 57 | 58 | ####Get list by name: 59 | ``` 60 | $spService.lists.getByTitle('My List').value().then( 61 | function (data) { 62 | //do stuff 63 | }, 64 | function (sender, args) { 65 | console.log('error'); 66 | } 67 | ); 68 | ``` 69 | 70 | ###Items 71 | ####Get list items from a specific list: 72 | ``` 73 | $spService.lists.getByTitle('My List').getItems().value().then( 74 | function (data) { 75 | //do stuff 76 | }, 77 | function (sender, args) { 78 | console.log('error'); 79 | } 80 | ); 81 | ``` 82 | 83 | ####Get list items from a specific list: 84 | ``` 85 | $spService.lists.getByTitle('My List').getItems().value().then( 86 | function (data) { 87 | //do stuff 88 | }, 89 | function (sender, args) { 90 | console.log('error'); 91 | } 92 | ); 93 | ``` 94 | 95 | ###Fields 96 | ####Get field from a specific list: 97 | ``` 98 | $spService.lists.getByTitle('My Task List').getFieldByName('Task Status').value().then( 99 | function (data) { 100 | //do stuff 101 | }, 102 | function (sender, args) { 103 | console.log('error'); 104 | } 105 | ); 106 | ``` 107 | 108 | -------------------------------------------------------------------------------- /angular-sharepoint.js: -------------------------------------------------------------------------------- 1 | // DIRECTIVE IDEAS 2 | // - 3 | 4 | // SERVICE IDEAS 5 | // - perform REST get commands with selects, filters, etc. 6 | // - perform JSOM for taxonomy & things REST can't do. 7 | // - provide the same syntax to user so they don't care if it's REST or JSOM 8 | 9 | (function (window, angular, undefined) { 10 | 'use strict'; 11 | 12 | var sp = angular.module('ngSP', []); 13 | 14 | sp.service('$spUtilityServices', function () { 15 | var self = this; 16 | self.getQueryStringParameter = function (paramToRetrieve) { 17 | var params = 18 | document.URL.split("?")[1].split("&"); 19 | var strParams = ""; 20 | for (var i = 0; i < params.length; i = i + 1) { 21 | var singleParam = params[i].split("="); 22 | if (singleParam[0] == paramToRetrieve) 23 | return singleParam[1]; 24 | } 25 | } 26 | self.getScripts = function (base, libraries, index, exec) { 27 | if (index === libraries.length) { 28 | exec(); 29 | } 30 | else { 31 | var lib = libraries[index]; 32 | index++; 33 | $.getScript(base + lib, function () { 34 | this.getScripts(base, libraries, index, exec); 35 | }); 36 | } 37 | } 38 | self.logError = function (jqXHR, textStatus, errorThrown) { 39 | if (jqXHR.hasOwnProperty('responseJSON')) { 40 | console.error(jqXHR.responseJSON.error.message.value); 41 | } 42 | else { 43 | console.error(jqXHR.responseText); 44 | } 45 | } 46 | self.ajaxQuery = function (url) { 47 | var d = $.Deferred(); 48 | $.ajax({ 49 | url: url, 50 | method: "GET", 51 | async: false, 52 | headers: { 53 | "Accept": "application/json; odata=verbose" 54 | }, 55 | success: function (data) { 56 | d.resolve(data); 57 | }, 58 | error: function (jqXHR, textStatus, errorThrown) { 59 | self.logError(jqXHR, textStatus, errorThrown); 60 | } 61 | }); 62 | return d.promise(); 63 | } 64 | }); 65 | 66 | sp.service('$spService', ['$spConstants', '$spUtilityServices', function ($spConstants, $spUtilityServices) { 67 | var self = this; 68 | self.lists = { 69 | outurl: $spConstants.appWebUrl + "/_api/SP.AppContextSite(@TargetSite)/web/lists?" + 70 | "@TargetSite='" + $spConstants.hostWebUrl + "'", 71 | getByTitle:function(listtitle){ 72 | this.outurl = this.outurl.replace("lists?", "lists/getByTitle(@TargetList)?"); 73 | this.outurl += "&@TargetList='" + listtitle + "'"; 74 | return this; 75 | }, 76 | getByBaseTemplateType: function (id) { 77 | this.outurl = this.outurl.replace("?", "?$filter=BaseTemplate eq " + id + "&"); 78 | return this; 79 | }, 80 | getTaskLists: function(){ 81 | return this.getByBaseTemplateType('171'); 82 | }, 83 | getItems: function(){ 84 | this.outurl = this.outurl.replace("?", "/items?"); 85 | return this; 86 | }, 87 | getFieldByName: function (field) { 88 | this.outurl = this.outurl.replace("?", "/fields?$select=Choices&$filter=Title eq '" + field + "'&"); 89 | return this; 90 | }, 91 | value: function () { 92 | var fetch = this.outurl; 93 | this.outurl = $spConstants.appWebUrl + "/_api/SP.AppContextSite(@TargetSite)/web/lists?" + 94 | "@TargetSite='" + $spConstants.hostWebUrl + "'"; 95 | return $spUtilityServices.ajaxQuery(fetch); 96 | } 97 | }; 98 | }]); 99 | 100 | sp.factory('$spConstants', function ($spUtilityServices) { 101 | var obj = {}; 102 | obj.hostWebUrl = decodeURIComponent($spUtilityServices.getQueryStringParameter('SPHostUrl')); 103 | obj.appWebUrl = decodeURIComponent($spUtilityServices.getQueryStringParameter('SPAppWebUrl')); 104 | obj.scriptBase = obj.hostWebUrl + "/_layouts/15/"; 105 | return obj; 106 | }); 107 | 108 | })(window, window.angular); 109 | --------------------------------------------------------------------------------