我们正在尝试使用Microsoft SharePoint中的REST API对现有主题的讨论给予答复。
以下是请求详细信息。
URL:https://[TenantName].sharepoint.com/_api/web/lists/GetByTitle('dicussion board')/Items
Headers: Authorization:Bearer XXXX
Content-Type:application/json;odata=verbose
accept:application/json;odata=verbose
RequestBody:
{
"Body": "<p>Test message using api</p>",
"__metadata": {
"type": "SP.Data.dicussion_x0020_boardListItem"
},
"ParentItemID":10,
"ContentTypeId": "0x0000EE0020063889D45FD823B4A8455444117056EE3"
}
但这在Sharepoint中创建了一个新主题。
有人可以帮忙吗?
答案 0 :(得分:0)
根据我的测试,一个电话无法做到这一点。
the script工作正常。
<script type="text/javascript">
function executeJson(options) {
var headers = options.headers || {};
var method = options.method || "GET";
headers["Accept"] = "application/json;odata=verbose";
if (options.method == "POST") {
headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
}
var ajaxOptions =
{
url: options.url,
type: method,
contentType: "application/json;odata=verbose",
headers: headers
};
if ("data" in options) {
ajaxOptions.data = JSON.stringify(options.data);
}
return $.ajax(ajaxOptions);
}
function createListItem(webUrl, listTitle, payload) {
var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items";
return executeJson({
"url": url,
"method": 'POST',
"data": payload
});
}
function moveListItem(webUrl, listTitle, itemId, folderUrl) {
var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getItemById(" + itemId + ")?$select=FileDirRef,FileRef";
return executeJson({
"url": url
})
.then(function (result) {
var fileUrl = result.d.FileRef;
var fileDirRef = result.d.FileDirRef;
var moveFileUrl = fileUrl.replace(fileDirRef, folderUrl);
var url = webUrl + "/_api/web/getfilebyserverrelativeurl('" + fileUrl + "')/moveto(newurl='" + moveFileUrl + "',flags=1)";
return executeJson({
"url": url,
"method": 'POST'
});
});
}
function getParentTopic(webUrl, listTitle, itemId) {
var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getItemById(" + itemId + ")/Folder";
return executeJson({
"url": url,
});
}
function createNewDiscussionReply(webUrl, listTitle, messagePayload) {
var topicUrl = null;
return getParentTopic(webUrl, listTitle, messagePayload.ParentItemID)
.then(function (result) {
topicUrl = result.d.ServerRelativeUrl;
return createListItem(webUrl, listTitle, messagePayload);
})
.then(function (result) {
var itemId = result.d.Id;
return moveListItem(webUrl, listTitle, itemId, topicUrl);
});
}
var listTitle = "Dicussion Board";
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var messagePayload = {
'__metadata': { "type": "SP.Data.Dicussion_x0020_BoardListItem" }, //set DiscussionBoard entity type name
'Body': "Test message using api", //message Body
'FileSystemObjectType': 0, //setto 0 to make sure Mesage Item
'ContentTypeId': '0x0107001F110388B0388441BE3F004885E844F9', //set Message content type
'ParentItemID': 1 //set Discussion (topic) Id
};
function CreateReply() {
createNewDiscussionReply(webUrl, listTitle, messagePayload)
.done(function (item) {
console.log('Message(reply) has been sent');
})
.fail(function (error) {
console.log(JSON.stringify(error));
});
}
</script>