Creating a Post on Message Boards Programmatically

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

Blogs
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
It looks like you're missing the userId and the userName in the addMessage call. Also there's a typo in the List argument.
Thanks PAULO..By using this API I am able to add comment in MBMessage table and even I can see the one entry in assetEntry table but when I am trying to fetch those message by using <liferay-ui:discussion> tag ,I am not getting the comment which we have add programatically ..but if I am using tablib to post a comment I can see the message on my page..

Do I am missing anything ,Any help would be appreciated.
Thanks PAULO..By using this API I am able to add comment in MBMessage table and even I can see the one entry in assetEntry table but when I am trying to fetch those message by using <liferay-ui:discussion> tag ,I am not getting the comment which we have add programatically ..but if I am using tablib to post a comment I can see the message on my page..

Do I am missing anything ,Any help would be appreciated.

Custom canvas prints that look goods in the room or playroom for gaming and programmer and there you could find best custom canvas online at https://www.tiaracle.com/collections/custom-canvas-prints