使用phonegap更新SharePoint列表项

时间:2016-05-23 05:17:19

标签: cordova azure sharepoint phonegap-plugins

我可以使用azure ad和azure-activedirectory-library-for-cordova对sharepoint进行身份验证。我能够从sharepoint获取列表但我无法更新或创建列表项。这是我验证和获取列表的代码。我需要的是创建或更新列表项。我正在考虑This example

var authority = "https://login.windows.net/common",
redirectUri = "http://my re direct url",
resourceUri = "https://my resource url",
clientId = "5a9hh56u-2485-7523-0122-j5k62463ab05",

var app = {
// Invoked when Cordova is fully loaded.
onDeviceReady: function() {
    document.getElementById('search').addEventListener('click', app.search);
},
// initialize authentication operations.
search: function () {
    app.authenticate(function (authresult) {

        app.requestData(authresult);
    });
},
// Shows user authentication dialog if required.
authenticate: function (authCompletedCallback) {

    app.context = new Microsoft.ADAL.AuthenticationContext(authority);
    app.context.tokenCache.readItems().then(function (items) {
        if (items.length > 0) {
            authority = items[0].authority;
            app.context = new Microsoft.ADAL.AuthenticationContext(authority);
            alert(app.context.webAbsoluteUrl);
        }
        // Attempt to authorize user silently
        app.context.acquireTokenSilentAsync(resourceUri, clientId)
        .then(authCompletedCallback, function () {
            // We require user cridentials so triggers authentication dialog
            app.context.acquireTokenAsync(resourceUri, clientId, redirectUri)
            .then(authCompletedCallback, function (err) {
                app.error("Failed to authenticate: " + err);
            });
        });
    });

},
// Makes Api call to receive user list.
requestData: function (authResult) {
    alert("Token : "+authResult.accessToken);
    var req = new XMLHttpRequest();
    var url = "https://mytenant.sharepoint.com/_api/web/lists/getbytitle('WebServiceTest')/items";
    req.open("GET", url, true);
    req.setRequestHeader('Authorization', 'Bearer ' + authResult.accessToken);

    req.onload = function(e) {
        if (e.target.status >= 200 && e.target.status < 300) {
            alert("target"+e.target.request);
            var xml = $.parseXML(e.target.response),
            $xml = $( xml ),
            $test = $xml.find('Title');
            alert($test.text());
            $("#userlist").text($test.text());
            return;
        }
        app.error('Data request failed: ' + e.target.response);
    };
    req.onerror = function(e) {
        app.error('Data request failed: ' + e.error);
    }

    req.send();
},

error: function(err) {
    var userlist = document.getElementById('userlist');
    userlist.innerHTML = "";
    var errorItem = document.createElement('li');
    errorItem.classList.add('topcoat-list__item');
    errorItem.classList.add('error-item');
    errorItem.innerText = err;
    userlist.appendChild(errorItem);
}    
};

   document.addEventListener('deviceready', app.onDeviceReady, false);

1 个答案:

答案 0 :(得分:0)

您可能需要执行POST来更新/创建项目而不是获取。像这样:

$.ajax({
    url: url + "/_api/web/lists/getbytitle('WebServiceTest')/items",
    type: "POST",
    contentType: "application/json;odata=verbose",
    data: JSON.stringify(item),
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data) {
        success(data); // Returns the newly created list item information
    },
    error: function (data) {
        failure(data);
    }
});