Cordova 使用phonegap更新SharePoint列表项
我可以使用azure ad和进行sharepoint身份验证。我可以从sharepoint获取列表,但无法更新或创建列表项。这是我用来验证和获取列表的代码。我需要的是创建或更新列表项。我是指Cordova 使用phonegap更新SharePoint列表项,cordova,azure,sharepoint,phonegap-plugins,Cordova,Azure,Sharepoint,Phonegap Plugins,我可以使用azure ad和进行sharepoint身份验证。我可以从sharepoint获取列表,但无法更新或创建列表项。这是我用来验证和获取列表的代码。我需要的是创建或更新列表项。我是指 var权限=”https://login.windows.net/common", 重定向URI=”http://my 重新定向url“, 资源URI=”https://my 资源url“, clientId=“5a9hh56u-2485-7523-0122-j5k62463ab05”, 变量应用={ //
var权限=”https://login.windows.net/common",
重定向URI=”http://my 重新定向url“,
资源URI=”https://my 资源url“,
clientId=“5a9hh56u-2485-7523-0122-j5k62463ab05”,
变量应用={
//当Cordova完全加载时调用。
ondevicerady:function(){
document.getElementById('search')。addEventListener('click',app.search');
},
//初始化身份验证操作。
搜索:函数(){
app.authenticate(函数(authresult){
app.requestData(authresult);
});
},
//如果需要,显示用户身份验证对话框。
身份验证:函数(authCompletedCallback){
app.context=新的Microsoft.ADAL.AuthenticationContext(授权);
app.context.tokenCache.readItems().then(函数(项){
如果(items.length>0){
权限=项[0]。权限;
app.context=新的Microsoft.ADAL.AuthenticationContext(授权);
警报(app.context.webAbsoluteUrl);
}
//尝试以静默方式授权用户
app.context.acquireTokenSilentAsync(resourceUri,clientId)
.then(authCompletedCallback,函数(){
//我们需要用户认证,因此会触发身份验证对话框
acquireTokenAsync(resourceUri、clientId、redirectUri)
.then(authCompletedCallback,函数(err){
应用程序错误(“验证失败:+err”);
});
});
});
},
//进行Api调用以接收用户列表。
requestData:函数(authResult){
警报(“令牌:+authResult.accessToken”);
var req=新的XMLHttpRequest();
变量url=”https://mytenant.sharepoint.com/_api/web/lists/getbytitle('WebServiceTest')/items”;
请求打开(“获取”,url,true);
请求setRequestHeader('Authorization','Bearer'+authResult.accessToken);
req.onload=功能(e){
如果(e.target.status>=200&&e.target.status<300){
警报(“目标”+e.target.request);
var xml=$.parseXML(e.target.response),
$xml=$(xml),
$test=$xml.find('Title');
警报($test.text());
$(“#userlist”).text($test.text());
返回;
}
应用程序错误('数据请求失败:'+e.target.response);
};
req.onerror=功能(e){
app.error('数据请求失败:'+e.error);
}
请求发送();
},
错误:函数(err){
var userlist=document.getElementById('userlist');
userlist.innerHTML=“”;
var errorItem=document.createElement('li');
errorItem.classList.add('topcoat-list__项目');
errorItem.classList.add('error-item');
errorItem.innerText=err;
userlist.appendChild(errorItem);
}
};
文件。添加的监听器('deviceready',app.onDeviceReady,false);
您可能需要发布一篇文章来更新/创建项目,而不是获取。大概是这样的:
$.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);
}
});
您可能需要通过POST更新/创建项目,而不是get。大概是这样的:
$.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);
}
});