« Back

Creating a Post on Message Boards Programmatically

Company Blogs April 23, 2014 By Paulo Fernandes

Hi guys

Recently I created programmatically a post on the Message Boards Portlet and I would like to share this knowledge with you.

The following code retrieve all categories from the Message Boards Portlet.

List categories = MBCategoryLocalServiceUtil.getCategories(themeDisplay.getScopeGroupId());
for(MBCategory category : categories ){
     category.getName(); // prints category name
}

To create a post we need the category id, so with the code above you can iterate the categories and select the appropriated category id.

When you have the category, you only need two more parameters, the title and the content of the post. Both are strings.

final long categoryId =	ParamUtil.get(request, "category_id", 0);
final String title = ParamUtil.get(request, "title", StringPool.BLANK);
final String question = ParamUtil.get(request, "question", StringPool.BLANK);

ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
MBMessage message = MBMessageServiceUtil.addMessage( themeDisplay.getScopeGroupId(), categoryId, title, question, "html", new ArrayList>(), false, 3.0, false, ServiceContextFactory.getInstance(request));	
System.out.println( message.getPrimaryKey() );	

 I recommend you to see the method signature.

If you have any doubts, please post in our forum.

Good luck

Threaded Replies Author Date
Very Good Sharing PAULO. Keep up the good work.... Ahamed Hasan May 8, 2014 4:04 PM
It looks like you're missing the userId and the... Joseph Toman January 14, 2015 10:48 PM

Very Good Sharing PAULO. Keep up the good work. Like this example about we can make use of any Liferay API's to implement any of the custom functionality. Literally, a whole new product can be evolved out of Liferay just by making use of these API's and not creating any new tables to capture data.

Ahamed Hasan
Author, Liferay Cookbook
Posted on 5/8/14 4:04 PM.
It looks like you're missing the userId and the userName in the addMessage call. Also there's a typo in the List argument.
Posted on 1/14/15 10:48 PM.