2
votes

I'm looking for some tips on how I can create a discussion reply using the 2013 Sharepoint REST end point. I'm not using the built in SP javascript libraries, instead accessing the REST end point directly using jQuery ajax calls.

My issue when attempting to create a reply is that it is creating the article as a new thread instead of a reply. I've searched around the web and all I can come up with is something to do with the URL path.

If I use the "sharepointEndPoint/_api/web/lists/getByTitle('discussions')/Items" url, it will create article as a new thread.

I've tried appending the ID of the parent thread on the end of items in brackets "(1)" for example and also "/title of parent thread" but both throw an error.

I'm also setting the ParentItemID and the ParentFolderId against the article, but sharepoint still creates it as a new thread instead of a reply.

1
any update? me too facing same issue like 'setting the ParentItemID and the ParentFolderId against the article, but sharepoint still creates it as a new thread instead of a reply.'? - user2395176

1 Answers

1
votes

ParentItemID property could not be specified via message payload since it is a read only property, it means the following query for creating a message 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 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);
    });
}