Working with Discussions List via SharePoint Client Object Model

Overview

In this post  will be demonstrated how to perform a common CRUD operations when working with Discussions List via  Client Object Model (JavaScript CSOM) in SharePoint 2010/2013.

Create a Discussion

So, let’s get started with creating a Discussion item. SP.Utilities.Utility.createNewDiscussion method is used for creating a discussion item:

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

view raw
createDiscussion.js
hosted with ❤ by GitHub

Load all Discussions

Since Discussion Content Type derives from Folder Content Type  we could utilize SP.CamlQuery.createAllFoldersQuery method to construct a query to retrieve all the discussions:

function getDiscussions(listTitle,OnItemsLoaded,OnError)
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
context.load(list);
var qry = SP.CamlQuery.createAllFoldersQuery();
var discussionItems = list.getItems(qry);
context.load(discussionItems);
context.executeQueryAsync(
function() {
OnItemsLoaded(discussionItems);
},
OnError
);
}

view raw
getDiscussions.js
hosted with ❤ by GitHub

Create a Message

The SP.Utilities.Utility.createNewDiscussionReply method is used to create a Message item (add reply to a discussion item):

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

view raw
createMessage.js
hosted with ❤ by GitHub

Load all Messages

Since message items  are contained within discussion container (Folder) , to identify messages by discussion we will use SPBuiltInFieldId.ParentFolderId field in CAML query:

function getMessages(listTitle,disscussionId,OnItemsLoaded,OnError)
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
context.load(list);
var qry = createAllMessagesByDisscussionIDQuery(disscussionId);
var messageItems = list.getItems(qry);
context.load(messageItems);
context.executeQueryAsync(
function() {
OnItemsLoaded(messageItems);
},
OnError
);
}
function createAllMessagesByDisscussionIDQuery(disscussionId) {
var qry = new SP.CamlQuery;
var viewXml = "<View Scope='Recursive'> \
<Query> \
<Where> \
<Eq> \
<FieldRef Name='ParentFolderId' /> \
<Value Type='Integer'>" + disscussionId + "</Value> \
</Eq> \
</Where> \
</Query> \
</View>";
qry.set_viewXml(viewXml);
return qry;
};

view raw
getMessages.js
hosted with ❤ by GitHub

Import Discussion List

And finally in order to see how to work with Discussions List in action,  let’s discuss one more example.  Suppose we need to import Discussions List content from an external source, let’s say from Stack Exchange. To access Stack Exchange content (Questions and Answers in our case) we will utilize Stack Exchange API:

function DiscussionBoardImporter()
{
this.sourceQuery = '/2.1/questions?order=desc&sort=votes&site=sharepoint&filter=!-.AG)sL*gPlV';
this.targetDiscussionsListName = 'Discussions List';
}
DiscussionBoardImporter.prototype = (function() {
var importData = function(data,discussionsListName)
{
$.each(data, function (i, question) {
var discussionProperties = {'Body': question.body,'Subject':question.title,'VoteNumber': question.score};
createDiscussion(discussionsListName,discussionProperties,
function(discussionItem){
console.log('Discussion ' + discussionItem.get_item('Title') + ' has been created successfully');
var messagesProperties = [];
$.each(question.answers, function (j, answer) {
messagesProperties.push({'Title': answer.title,'Body': answer.body});
});
createMessages(discussionItem,messagesProperties,
function(){
console.log('Messages have been created successfully');
},
function(sender,args){
console.log('Error occured while creating messages:' + args.get_message());
}
);
},
function(sender,args){
console.log('Error occured while creating disscussion:' + args.get_message());
});
});
};
var loadData = function(query,targetListName, loaded)
{
$.ajax({
url: 'http://api.stackexchange.com&#39; + query,
dataType: 'json',
success: function(data) {
loaded(data.items,targetListName);
}
});
};
return {
constructor:DiscussionBoardImporter,
import: function()
{
loadData(this.sourceQuery,this.targetDiscussionsListName,importData);
}
};
})();
var importer = new DiscussionBoardImporter();
importer.import();

5 thoughts on “Working with Discussions List via SharePoint Client Object Model

  1. Please elaborate your code, what should be taken as “DiscussionItem” and “Properties” in the example of creating a message. also what is propName?

  2. For the “Create A Discussion” code, I am getting “‘SP.ClientContext.get_current’ is null or not an object” so do you know which JS files need to be linked/included on the page?

  3. Thank you for the post it was helpful! Do you know how I can render the messages within a discussion folder? Once I have all the messages returned in the query, how do I render them similar to Flat.aspx?
    I am currently trying to inject the flat.aspx?IsDlg=1&Folder etc.. ‘all messages for a discussion item’ view into my dashboard using jquery load().

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.