2016-05-23 2 views
1

Я могу выполнить аутентификацию в sharepoint с помощью azure ad и azure-activedirectory-library-for-cordova. Мне удалось получить список из sharepoint, но я не смог обновить или создать элемент списка. Это мой код для аутентификации и получения списка. Мне нужно создать или обновить элемент списка. Я В This exampleОбновление элемента списка SharePoint с помощью phonegap

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); 

ответ

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); 
    } 
}); 
Смежные вопросы