List Facebook friends and groups post to their walls with graph API in JSF/PrimeFaces the simple way

Friday August 19, 2011 ()

Before we continue please make sure you already have a Facebook App and an access_token. Please visit here on how to get an access_token from your JSF application with the necessary access permissions. Please visit here to learn more about Facebook access permissions.

To make everything easy and simple we use restfb (get it here). It is an encapsulation of most of Facebook API. Aside from just posting to walls we also make sure that we include an action link. An additional link back to your site. The action link is located beside the standard "like" and "comment" links of a post similar to the "Get Bonus" from the graphic below.

List Facebook friends and groups post to their 

walls with graph API in JSF/PrimeFaces the simple way

Here is the important part of a method in our backing bean. It should work with PrimeFaces p:dataGrid. Please note that we are using Facebook Graph. We post direct without the Facebook dialog prompt. Let us assume the containing method of the snippet below is called getFacebook() returning List.

  List list = new ArrayList ();

   try {

      // Link back to your site from a Facebook wall post.  
      // Must be from domain of Facebook app.

      String actionLink = 

      // scope should be in the permission 
      // of your access_token.  

      actionLink = 
            + "redirect_uri=" + actionLink + "&"
            + "scope=publish_stream,user_groups,status_update&"
            + "response_type=token";

      // actions in Facebook is a JSON string and should 
      // URL encoded. 

      Map map = new HashMap();
      map.put("name", "Buy Now");
      map.put("link", actionLink);

      DefaultJsonMapper obj = new DefaultJsonMapper();
      String actions = obj.toJson(map);
      actions =, "UTF-8");

      // We use restfb in communicating with Facebook

      FacebookClient facebookClient = 
                new DefaultFacebookClient(token);

      // Groups

      com.restfb.Connection<Group> gr = 
             ("me/groups", Group.class);

      List<Group> g = gr.getData();
      int len = g.size();

      for (int i = 0; i < len; i++) {

          Group G = g.get(i);
          String groupName = G.getName();
          String groupId = G.getId();
          String groupPicture = 
               "" + groupId+ "/picture";
	   Info info = new Info();

           info.setGroupName (groupName);
	   info.setGroupId (groupId);
           info.setGroupPicture (groupPicture); 


              Scroll down below on how to use the Facebook
              Feed dialog instead of graph.


      // Access your friends

      com.restfb.Connection<User> my = 
           ("me/friends", User.class);
      List<User> u = my.getData();

      // Do same as with groups above, 
      //  loop through to update list wit Info objects

    } catch (Exception e) {


    return list;


Below is how the p:dataGrid should look like

<p:dataGrid paginator="false" style="width:850px;"
    var="bff" value="#{yourBean.facebook}" 


           <h:panelGrid >
              <h:graphicImage value ="#{bff.groupPicture}" />
              <h:outputText value="#{bff.groupName}" 
                style="font-size:10px;font-weight: bold" />

           <f:param name="profileid" value="#{bff.groupId}" />
           <f:param name="token" value="#{bff.token}" />
           <f:param name="url" value="#{}" />
           <f:param name="picture" value="#{bff.picture}" />
           <f:param name="desc" value="#{bff.description}" />
           <f:param name="name" value="#{}" />
           <f:param name="actions" value="#{bff.actions}" />



Below is the Info class and the publish method. Please note that the publish method is part of our bean not of the Info class.

 public class Info {
     private String groupName;
     private String groupId;

     public String getGroupName () {
          return groupName;

     public void setGroupName (String s) {
        groupName = s;



     public void publish (Action event) {

        FacesContext context = FacesContext.getCurrentInstance();
        Map rmap = 
        String token = (String) rmap.get("token");
        String id = (String) rmap.get("profileid");
        String name = (String) rmap.get("name");
        String desc = (String) rmap.get("desc");
        String link = (String) rmap.get("url");
        String picture = (String) rmap.get("picture");
        String actions = (String) rmap.get("actions");

        FacebookClient fbc = new DefaultFacebookClient(token_);

        FacebookType response =
           fbc.publish(id_ + "/feed", FacebookType.class,
           Parameter.with("app_id", "YOUR-APP-ID"),
           Parameter.with("access_token", token),
           Parameter.with("link", link),
           Parameter.with("picture", picture),
           Parameter.with("description", desc),
           Parameter.with("name", name),
           Parameter.with("actions", actions),
           Parameter.with("message", " ")); 

        String mess = response.toString();

        context.addMessage(null, new FacesMessage(mess, ""));


For some reasons that you wanted to post to Facebook through Facebook Feed dialog. Below is how.

    String F = "
    + "redirect_uri=" + YOUR-REDIRECT-URL + "&"
    + "access_token=" + token + "&" 
    + "link=" + url + "&"
    + "picture=" + picture + "&"
    + "description=" + desc + "&"
    + "to=" + id + "&"
    + "name=" + name + "&"
    + "actions=" + actions;

Set the string above as return value in a method of our Info class, we call that method set/getClick for the sake of this blog. Below is the p:dataGrid corresponding to the above change.

 <p:dataGrid paginator="false" style="width:850px;"
    var="bff" value="#{yourBean.facebook}" 


       <h:outputLink value = "#{}" >    
           <h:panelGrid >
              <h:graphicImage value ="#{bff.groupPicture}" />
              <h:outputText value="#{bff.groupName}" 
                style="font-size:10px;font-weight: bold" />



Good luck.


Comments (List Facebook friends and groups post to their walls with graph API in JSF/PrimeFaces the simple way )