Question

How can I dynamically get the ID of the current discussion thread in javascript?
Currently, I have the following code:

function submitReply() {
    var ctx = new SP.ClientContext.get_current();
    var discussionItem = ctx.CurrentItem;
    var body = CKEDITOR.instances['commentArea'].getData();

    var messageProperties = {'Body': body}; 
    createMessage(discussionItem,messageProperties,
        function(){
            console.log('Reply has been post successfully');
        },
        function(sender,args){
             console.log('Error occurred while posting a reply:' + args.get_message());
        }
    );
}

enter image description here

When I use ctx.CurrentItem the return value is null.

enter image description here

Here is the link to my complete code: https://pastebin.com/jNFbUBaB

Was it helpful?

Solution

You can get it from ctx. See more about ctx.

Current Discussion Item

var discussionItem = ctx.CurrentItem;

Current Discussion Item ID

var discussionItem = ctx.CurrentItem.ID;

You need to ensure that sp.js is load in the page.

SP.SOD.executeFunc('sp.js', null, function() {
   submitReply();
});

No need of line var ctx = new SP.ClientContext.get_current();

Update

If discussion thread contains any reply, then ctx.CurrentItem does not work. In that case, solution is:

var discussionItem = ctx.ListData.Row[0];

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top