3

I am trying to find a way to post a reply to an existing discussion on a Discussion board using the REST API.

I cannot find a way to do this. I can only create a new discussion topic using REST. I have tried submitting a post request containing a ParentItemID in the body but that still creates a new discussion not a reply. Is there any way to do this using REST? The only examples I've seen use JSOM.

This is a similar question, but it got no responses.

Adding Discussion board replies using REST API

  • have you get any solution for this. – Vishal Thakkar Aug 8 '16 at 11:56
2

In fact ParentItemID property could not be specified since it is a read only property, it means the following query for creating a message (reply) item fails:

Url  /_api/web/lists/getbytitle('Discussions')/items
Method POST
Data { 
    '__metadata': { "type": "SP.Data.DiscussionsListItem" },
   'Body': "Message text goes here",  
   'FileSystemObjectType': 0, 
   'ContentTypeId': '<MessageContentTypeId>', 
   'ParentItemID': <DiscussionItemId>   //can't be set since it is read only
}  

Solution

For creating a message (reply) under a discussion item (folder) you could consider following solution: once message item is created, it's getting moved under a discussion item (folder container)

Example

The following example demonstrates how to create a message (reply) in Discussion Board via SharePoint REST API:

var listTitle = "Discussions"; //Discussions Board title
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var messagePayload = {
   '__metadata': { "type": "SP.Data.DiscussionsListItem" },  //set DiscussionBoard entity type name
   'Body': "Message text goes here",  //message Body
   'FileSystemObjectType': 0, //set to 0 to make sure Message Item is created
   'ContentTypeId': '0x0107008822E9328717EB48B3B665EE2266388E', //set Message content type
   'ParentItemID': 123   //set Discussion item (topic) Id
};

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

where

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);
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.