By Bryan Leboff
One of the lesser publicized development features in the last few Salesforce releases has been Chatter in Apex or the Connect API. These apex classes give you access to chatter in through apex in much the same way as the REST API allowing you to create and customize your own chatter feeds and create chatter posts. This takes a lot of the complexity out of customization and creating your own chatter feeds in Visualforce.
These classes could come in handy when the out of the box chatter components provided in Visualforce (chatter:newesfeed and chatter:feed) do not match the branding of your Force.com site, or further cusotmizations are needed to the displayed feeds. For example the Connect API allows for filtering based on object prefix, which is not provided in these out of the box components.
Output / Input
There are two types of ConnectAPI classes, output and input classes. This blog will focus mostly on output classes to display a chatter feed. (Creating a post via the input classes could be featured in a future blog post.)
Chatter feeds are basically just a collection of the class ‘FeedItem’ I have created a rough UML diagram of some of the classes/relationships in a feed item that I will cover note: this is not comprehensive, and there are other classes associated with feed items.
There are two main methods in the Connect API Chatter Feeds Class (and many overloads to these methods) for getting the chatter feed item page.
Displaying a Newsfeed
Now it’s time to get our hands dirty and take a deeper look at some code. The first thing we will want to do is create a Visualforce page and controller. Now in the controller we will use the method
Where the first string is the network or community id (or null for none), then the feed type enum, and the last string is the subject (who’s feed to grab). So to grab our own newsfeed we will call
ConnectApi.ChatterFeeds.getFeedItemsFromFeed(null, ConnectApi.FeedType.News, ‘me’);
Pretty simple huh?
In order to display this feed on the Visualforce page, we must iterate over the feed items and display their data. As far as I can tell when generating the page the API is smart enough to grab all and only the data from the newsfeed requested by the page.
A simple example of this would be something like this:
As you can see we iterate over feedItemPage.items and pull out the creating users name, ID, and the feed item text. And just like that we’re displaying a chatter feed on our visualforce page.
As you may have noticed, we’re missing some few key features. Right now the feed will just be displayed as plain text, but as we know a chatter post may contain certain types of elements like hashtags and mentions. These elements are stored in the body attribute as “message segments.” The following are the current message segment classes:
I will focus on showing how to display a mention segment and text segment, but the process would be similar for any of the segments.
The message segment class contains the message text as plain text and a type field which stores the enum of the message segment type. Because we can’t access these enums in Visualforce, we’ll add properties to our controller that will return their values so that we can check against them in the Visualforce page.
Now back in our visualforce page, lets remove where we simply display feedItem.body.text and iterate over feedItem.body.messageSegments. In each iteration we will check the segment type, and appropriately display the data based on type.
For the MentionSegment we need to get a little clever. The Visualforce compiler will know the message segment class, does not have a ‘record’ property, but we are referencing the mentionsegment class that does have this property so it will not compile. The solution is to dynamically reference this property though using the syntax segment[‘record’].
Now we can see @mentions in our feed as links to the mentioned user!
The last remaining piece is displaying comments. Since we already know how to display feed items this shouldn’t be too hard. Although comments are a different class than a feeditem they are very similar as they both store their body as a FeedBody. Comments are stored on the comments property of the feeditem so all we need to do is iterate of the comments, and display them much like regular feeditems.
As with any Salesforce API there are some governor limits to be aware of. The rate limits match that of the Chatter REST API, and will throw a ConnectApi.RateLimitException if these limits are exceeded.
I have stored the final code for this project in this github repo:
As always leave any questions, concerns, glaring errors in the comments. Hope this helps get you started with the Connect API!