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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); | |
} |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); | |
} |
Create a Message
The SP.Utilities.Utility.createNewDiscussionReply method is used to create a Message item (add reply to a discussion item):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); | |
} |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' + 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(); |
Please elaborate your code, what should be taken as “DiscussionItem” and “Properties” in the example of creating a message. also what is propName?
Please help. Do you have any code samples that show how to make the calls to these functions?
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?
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().
Sorry, I do not know.