function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AlanisticAlanistic 

Handling URLs posted on chatter using api to display feed on a visualforce page

Hi,

I'm relative new to developing on Salesforce.

 

I've managed to create a visualforce page that pulls a specific chatter feed onto it. This displays the chatter message and who posted it.

 

My visual force code:

 

{!feedItem.body.text}<br/>
<i><b>Posted {!feedItem.relativeCreatedDate} by {!feedItem.actor.name}</b></i><br/>

 

 

The question I have is how can I handle URL's. Sometimes a post on chatter will include a link and so I want the address posted to appear on the visualforce as an actual URL.

 

Any suggestions?

 

Note: The code I'm using is heavily based on the quick start guide here

 

Thanks!

 

Alan

alouie_sfdcalouie_sfdc

feedItem.body.text is the text-only version of the feed item. To display a "rich" version of the feed item that could possibly include URLs, @-mentions, hashtags, and system-generated information, you need to use feedItem.body.messageSegments instead. It's a List of ConnectApi.MessageSegment that you need to iterate over.

 

For the API reference, see here:

http://www.salesforce.com/us/developer/docs/apexcode/Content/connectAPI_outputs.htm

 

Look for the ConnectApi.AbstractMessageBody class and the ConnectApi.MessageSegment class. Note that ConnectApi.MessageSegment also has a "text" property that you can use as a text-only version of a message segment if you encounter a message segment type that you don't know how to render.

AlanisticAlanistic

Thanks.  I had looked at that previously, but when I use the segments I get the below output on my page:

 

[ConnectApi.TextSegment[buildVersion=28.0, text=Test post with, type=Text], ConnectApi.LinkSegment[buildVersion=28.0, text=http://test.com, type=Link, url=http://www.test.com]]

 

Is there something I've overlooked?

 

Alternatively, what would the easiest way be to use the link attached to a chatter post?  This would mean the text displayed would link to the address in the link attachment.

alouie_sfdcalouie_sfdc

What you have there is feedItem.body.messageSegments converted to a String. Instead of that, you want to iterate over the message segments and render each one appropriately. Something like this pseudocode, but converted to VisualForce syntax:

 

for (ConnectApi.MessageSegment segment : feedItem.body.messageSegments) {

    if (segment instanceof ConnectApi.LinkSegment) {

        <a href="segment.url">segment.text</a>

    }

    else if (segment instanceof ConnectApi.SomeOtherSegmentType) {

    }

    :

    else {

        // Fall back to text

        segment.text

    }

}

 

If you want to render link attachments, it's similar. You need to take a look at the feedItem.attachment property, check to see if it's an instance of ConnectApi.LinkAttachment, and render it appropriately.