View Sidebar
Get discussion board replies using JSOM

Get discussion board replies using JSOM

December 9, 2013 16:00

As we already know how to connect to SharePoint 2013 using JSOM. We will take this further to load replies of a discussion thread dynamically.

Here we are retrieving replies body, date posted, poster name. Caml Queries are used to apply condition for retrieval list like row limit, descending/ascending order etc..

var list = context.get_web().get_lists().getByTitle('DISCUSSION_BOARD_NAME');
var camlQuery = SP.CamlQuery.createAllFoldersQuery();
camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name="DISCUSSION_FIELD_REF" /><Value Type="Text">'+ DISCUSSION_FIELD_REF_VALUE +'</Value></Eq></Where></Query></View>');
this.listItems = list.getItems(camlQuery);
context.load(listItems);
context.executeQueryAsync(function()
{
 var list = context.get_web().get_lists().getByTitle('DISCUSSION_BOARD_NAME');
 var camlQuery = SP.CamlQuery.createAllFoldersQuery();
 camlQuery.set_folderServerRelativeUrl(listItems.itemAt(0).get_item('FileRef').toString());
 camlQuery.set_viewXml('<OrderBy><FieldRef Name="Created" Descending="True"></FieldRef><RowLimit>3</RowLimit></OrderBy>');
 this.listItems = list.getItems(camlQuery);
 context.load(listItems);
 context.executeQueryAsync(function()
 {
 var listItemEnumerator = listItems.getEnumerator(); 
 while (listItemEnumerator.moveNext()) {
 var currentItem = listItemEnumerator.get_current(); 
 var dated = new Date(currentItem.get_item("Modified"));
 dated = dated.getDate() +" "+ monthArr[dated.getMonth()] +" "+ dated.getFullYear();

 console.log(currentItem.get_item("Body") +":"+ currentItem.get_item("Author").get_lookupValue() +":"+ dated)
 }
 },
 function()
 {
 });
},
function()
{
});
If you like the article please share

Leave a reply

You must be logged in to post a comment.