3

How can I reply to a discussion.

This is my add discussion code but not for reply.

$("#submitReply").click(function (){
    var newElement = new Object();
    newElement.Body = document.getElementById('reply_message').value;
    var activmeta = { Title: newElement.title, Body: newElement.Body }
    addListItem(_spPageContextInfo.webAbsoluteUrl, 'LFeed', activmeta,alert, '')
    return false;
});

to add reply, I tried to add the parentItemId, but it dosent work.

Any ideas

1

I copied below code from blog http://blog.vgrem.com/2014/01/20/working-with-discussions-list-via-sharepoint-client-object-model/

Create Discussion

function createDiscussion(listTitle,properties,OnItemAdded,OnItemError)
{
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();

    var list = web.get_lists().getByTitle(listTitle);
    context.load(list);

    var discussionItem = SP.Utilities.Utility.createNewDiscussion(context, list, properties.Subject);
    for(var propName in properties) {
       if(propName == 'Subject') continue; 
       discussionItem.set_item(propName, properties[propName]) 
    }
    discussionItem.update();
    context.load(discussionItem);

    context.executeQueryAsync(
        function() {
          OnItemAdded(discussionItem);           
        },
        OnItemError
    );
}

Creating messages

function createMessage(discussionItem,properties,OnItemAdded,OnItemError)
{
    var context = new SP.ClientContext.get_current();
    var messageItem = SP.Utilities.Utility.createNewDiscussionReply(context, discussionItem);
    for(var propName in properties) {
       messageItem.set_item(propName, properties[propName]) 
    }
    messageItem.update();
    context.executeQueryAsync(
        function() {
          OnItemAdded(messageItem);           
        },
        OnItemError
    );
}

function createMessages(discussionItem,messagesProperties,OnItemsAdded,OnItemsError)
{
    var context = new SP.ClientContext.get_current();
    var messageItems = [];
    $.each(messagesProperties, function (i, properties) {
       messageItems.push(SP.Utilities.Utility.createNewDiscussionReply(context, discussionItem));
       for(var propName in properties) {
          messageItems[i].set_item(propName, properties[propName]) 
       }
       messageItems[i].update();       
    });

    context.executeQueryAsync(
        function() {
          OnItemsAdded(messageItems);           
        },
        OnItemsError
    );
}
  • 4
    OP wants to add discussions/replies using REST API and not JSOM – Nadeem Yousuf Nov 24 '14 at 16:08
1

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

Example

var listTitle = "Discussions"; //Discussions Board title
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, //set to 0 to make sure Message Item is created
   'ContentTypeId': '0x0107008822E9328717EB48B3B665EE2266388E', //set Message content type
   'ParentItemID': 14  //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);
    });
}

The explanation

  • since a Discussion Board contains discussions and messages, the following properties needs to be specified for a message payload: FileSystemObjectType=0, ContentTypeId- content type Id of message item, ParentItemID - discussion item id
  • for setting parent discussion item, ParentItemID property of Message content type is used, but it cant be specified via payload since it is a read only property (the next item explains how message is created under a discussion)

  • since a discussion item (topic) is represented as a folder in Discussion Board list, this is what the method moveListItem is intended for, once message item is created, it getting moved under a discussion item

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.