Newer Version Available

This content describes an older version of this product. View Latest

Step 1: Get the Chatter Feed and Group Data

The first step is to create an Apex controller that uses Chatter in Apex to populate a drop-down list with the Chatter groups that the logged-in user is a member of. The controller also uses Chatter in Apex to get the feed for the selected group.
  1. Click Your Name | Developer Console.
  2. In the Developer Console, click File | New | Apex Class.
  3. Enter the name GroupFeedController and click OK.
  4. Copy this code and paste it into the GroupFeedController class, replacing the existing code:
    1swfobject.registerObject("clippy.codeblock-0", "9");global class GroupFeedController{
    2    
    3    // Declare and assign values to strings to use as method parameters.
    4    private static String communityId = null;
    5    private static String userId = 'me';
    6    
    7    // Holds the ID of the selected group.
    8    // Pass this property to getFeedItemsFromFeed to get the group's feed.
    9    global String groupId { get; set; }
    10 
    11    // Get the IDs and names for all of the groups 
    12    // the logged-in user is a member of. Add them to
    13    // a List of SelectionOption objects. This List populates
    14    // the drop-down menu in the GroupFeed custom component.
    15    global static List<SelectOption> getGroupOptions() {
    16        List<SelectOption> options = new List<SelectOption>();
    17        
    18        // Adds a blank option to display when the page loads.
    19        options.add(new SelectOption('', ''));
    20        
    21        // Declare and assign values to strings to use as method parameters. 
    22        Integer page = 0;        
    23        Integer pageSize = 100;
    24        
    25        // Use Chatter in Apex to get the names and IDs of every group
    26        // the logged-in user is a member of.
    27        // Chatter in Apex classes are in the ConnectApi namespace.
    28        // communityId -- a community ID or null.
    29        // userId -- the user ID or the keyword 'me' to specify the logged-in user.
    30        // page -- the page number to return.
    31        // pageSize -- the number of items on the page.
    32        ConnectApi.UserGroupPage groupPage = ConnectApi.ChatterUsers.getGroups(communityId, userId, page, pageSize);
    33        
    34        // The total number of groups the logged-in user is a member of.
    35        Integer total = groupPage.total;
    36        
    37        // Loop through all the groups and add each group's id and name
    38        // to the list of selection options.
    39        while (page * pageSize < total) {
    40            // groupPage.groups is a List of ConnectApi.ChatterGroupSummary objects.
    41            // ChatterGroupSummary is a subclass of ChatterGroup.
    42            // For each ChatterGroup object in the List...
    43            for (ConnectApi.ChatterGroup grp : groupPage.groups) {
    44                // Add the group's ID and name to the list of selection options.
    45                options.add(new SelectOption(grp.id, grp.name));
    46            }
    47           
    48            page++;
    49            
    50            if (page * pageSize < total) {
    51                // Get the next page of groups.
    52                groupPage = ConnectApi.ChatterUsers.getGroups(communityId, userId, page, pageSize);
    53            }
    54        }
    55        
    56        // Return the list of selection options.
    57        return options;
    58    }
    59    
    60    // Get the feed items that make up a group's feed.
    61    global List<ConnectApi.FeedItem> getFeedItems() {
    62        if (String.isEmpty(groupId)) { return null; }
    63        // To get the feed for a group, use the Record feed type and pass a group ID.
    64        // getFeedItemsFromFeed returns a ConnectApi.FeedItemPage class.
    65        // To get the List of ConnectApi.FeedItem objects,
    66        // add the .items property to the call.
    67        return ConnectApi.ChatterFeeds.getFeedItemsFromFeed(communityId, ConnectApi.FeedType.Record, groupId).items;
    68    }
    69    
    70    public PageReference choose() {
    71        return null;
    72    }
    73    
    74}
  5. Click File | Save.