Benutzer:MovGP0/SharePoint/AngularJS

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      


Loading AngularJS
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="application/ecmascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<div ng-app="myApp">
...
</div>
</asp:Content>
Controller
function TodoCtrl($scope, $SharePointJSOMService) {
    SP.SOD.executeOrDelayUntilScriptLoaded(myFunction, "SP.js");
    function myFunction() {
        var promise = $SharePointJSOMService.getTasksRESTAppWeb($scope, 'Tasks');
        promise.then(function (data, status, headers, config) {
            $scope.todos = [];
            angular.forEach(data.data.d.results, function (todo) {
                $scope.todo.push({
                    text: todo.Title,
                    dueDate: todo.DueDate,
                    priority: todo.Priority,
                    assignedTo: todo.AssignedTo.Title,
                    status: todo.Status,
                    done: todo.Status == 'Completed',
                    id: todo.ID
                });
            });
        }, function (data, status, headers, config) {
            console.log("Error " + status);
        });
    }
}
jQuery Promise
$.when(SharePointJSOMService.getCurrentUser())
 .done(function(jsonObject) {
     currentUser = jsonObject.d;
 })
 .fail(function(error) {
     console.info(JSON.stringify(error));
 });
Service
myApp.service('$SharePointJSOMService', function ($q, $http) {
    this.getTasksRESTAppWeb = function ($scope, listTitle) {
        JSRequest.EnsureSetup();
        hostweburl = decodeURIComponent(JSRequest.QueryString["SPHostUrl"]);
        appweburl = decodeURIComponent(JSRequest.QueryString["SPAppWebUrl"]);
 
        var restQueryUrl = appweburl + "/_api/web/lists/getByTitle('" + listTitle + "')/items?$select=Title,ID,DueDate,Status,Priority,AssignedTo/ID,AssignedTo/Title&$expand=AssignedTo/ID,AssignedTo/Title";
        return  $http({
            method: 'GET',
            url: restQueryUrl,
            headers: { "Accept": "application/json; odata=verbose" }
        });
    }
}
Service in Host Web
myApp.service('$SharePointJSOMService', function ($q, $http) {
    this.getTasksREST = function ($scope, listTitle) {
        var deferred = $.Deferred();
        //First we must call the EnsureSetup method
        JSRequest.EnsureSetup();
        hostweburl = decodeURIComponent(JSRequest.QueryString["SPHostUrl"]);
        appweburl = decodeURIComponent(JSRequest.QueryString["SPAppWebUrl"]);
 
        var restQueryUrl = appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getByTitle('" + listTitle + "')/items?$select=Title,ID,DueDate,Status,Priority,AssignedTo/ID,AssignedTo/Title&$expand=AssignedTo/ID,AssignedTo/Title&@target='" + hostweburl + "'";
 
        var executor = new SP.RequestExecutor(appweburl);
        executor.executeAsync({
            url: restQueryUrl,
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data, textStatus, xhr) {
                deferred.resolve(JSON.parse(data.body));
            },
            error: function (xhr, textStatus, errorThrown) {
                deferred.reject(JSON.stringify(xhr));
            }
        });
        return deferred;
    };
}