[英]Javascript with SharePoint 2013 REST Endpoint “INSERT” into List?
Here are what i'm going to use: 这是我要使用的:
Refs: 参考:
Very simply: 很简单:
I tried: 我试过了:
$.ajax({
url: "https://example.com/_api/web/lists/getbytitle('Announcements')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify( { '__metadata': { 'type': 'SP.Data.AnnouncementListItem' }, "Title": "New Announcement!" } ),
headers: {
"Accept": "application/json;odata=verbose",
"Authorization": "Bearer " + accessToken
"X-RequestDigest": form digest value,
"IF-MATCH": etag,
},
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
Then i know a lot of things go wrong especially in headers
section. 然后我知道很多事情出了问题,尤其是在headers
部分。 But what does it mean by: 但这意味着什么:
Authorization
accessToken
X-RequestDigest
IF-MATCH
.. and then HOW TO get these values (with JavaScript)? ..然后如何获取这些值(使用JavaScript)? So that: 以便:
I still can not find a simple and complete example about doing this Update
/ Insert
properly. 我仍然找不到有关正确执行此“ Update
/ Insert
的简单完整示例。
So there are two ways that I have used to submit an item to a list, the jQuery library SPServices and REST API's. 因此,我曾使用过两种方法将项目提交到列表中,即jQuery库SPServices和REST API。 SPServices is really well documented here . SPServices的确在这里有详细记录。 Using REST API's is much faster and pretty easy too! 使用REST API的速度更快,也非常容易!
function createListItem(itemProperties, success, failure) {
$.ajax({
url: "https://example.com/_vti_bin/listdata.svc/Announcements",
type: "POST",
processData: false,
contentType: "application/json;odata=verbose",
data: JSON.stringify(itemProperties),
headers: {
"Accept": "application/json;odata=verbose"
},
success: function(data) {
success(data.d);
},
error: function(data) {
// failure(data.responseJSON.error);
alert("error");
}
});
}
First thing I am doing above is creating a function that you can call whenever you want to create a new list item. 上面我要做的第一件事是创建一个函数,只要您要创建新的列表项,就可以调用该函数。 The parameter itemProperties can be populated with the fields which you need, see below. 可以使用所需字段填充参数itemProperties,请参见下文。
var Title = "Title";
var Answer = "Answer";
var userid = _spPageContextInfo.userId;
var taskProperties = {
'Title': Title,
'Answer': Answer,
'UserId': userid
};
Then all we have to do is call this function with the new variable we just declared. 然后,我们要做的就是使用刚刚声明的新变量调用此函数。
createListItem(taskProperties, function(task) {
alert("Thank you for your input!");
},
function(error) {
console.log(JSON.stringify(error));
}
);
Actually jsfiddle which you have posted in the previous commend is not the REST . 实际上,您在上一个建议中发布的jsfiddle不是REST。 you just use the SharePoint client object model. 您只需使用SharePoint客户端对象模型。 find below the REST API model I hope it will work 在REST API模型下面找到我希望它可以工作
var cat = {
"__metadata": { "type": ItemType },
"Title": "GenIT-Issue",
}
$.ajax({
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('Tickets')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(cat),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
},
error: function (data) {
}
});
I run this code inside my SharePoint page so there is no authentication required. 我在SharePoint页面中运行此代码,因此不需要身份验证。 it will run on current user privilege 它将以当前用户权限运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.