Skip to content

Instantly share code, notes, and snippets.

@vgrem
Created September 12, 2016 12:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrem/c5327ddbb1ceede884c14e35bb21117e to your computer and use it in GitHub Desktop.
Save vgrem/c5327ddbb1ceede884c14e35bb21117e to your computer and use it in GitHub Desktop.
Demonstrates how to create a message (reply) in Discussion Board via SharePoint REST API
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);
});
}
@vgrem
Copy link
Author

vgrem commented Sep 12, 2016

Usage

var listTitle = "Discussions";
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var messagePayload = {
   '__metadata': { "type": "SP.Data.DiscussionsListItem" },  //set DiscussionBoard entity type name
   'Body': "Thanks for the information",  //message Body
   'FileSystemObjectType': 0, //setto 0 to make sure Mesage Item
   'ContentTypeId': '0x0107008822E9328717EB48B3B665EE2266388E', //set Message content type
   'ParentItemID': 14  //set Discussion (topic) Id
};


createNewDiscussionReply(webUrl,listTitle,messagePayload)
.done(function(item)
{
    console.log('Message(reply) has been sent');
})
.fail(function(error){
    console.log(JSON.stringify(error));
});

@ScotBoy
Copy link

ScotBoy commented Oct 19, 2017

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.

@KevinTriplett
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment