-
-
Save vgrem/c5327ddbb1ceede884c14e35bb21117e to your computer and use it in GitHub Desktop.
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); | |
}); | |
} |
ParentItemID is used to store the relationship between discussion replies. Your method works when the reply is to the main post (that is, the main discussion post is a folder and the reply is a message under this folder). However, it does not work when the reply is threaded below another reply. In this case, the ParentItemID does not point to a folder, but to a message. In this case, function getParentTopic fails.
Let's say I have a discussion board 'GroupDiscussion'.
I post a message ('Hello everyone') to this board, and it gets ID=1 (FSObjType=1)
I post a reply to 'Hello Everyone', which gets an ID=2 (FSObjType=0, ParentItemID=1, ParentFolderID=1)
I post a reply to this reply, which gets ID=2 (FSObjType=0, ParentItemID=2, ParentFolderID=1).
From what I have found, threaded replies cannot be posted via REST API. ParentItemID cannot be set and 'Threading' is not populated.
If you have any suggestions, I would love to hear them as I cannot get around this problem.
While this appears to work (the reply shows up in the discussion thread of the parent post) it does not set the ParentItemID
property on the reply, it remains null
which breaks some of the features of discussions. See this article: REST api support for discussion list
Usage