Thursday, 7 July 2011

Chatter On Vfpage

Hai friends,In my last blog, i have explained you objects involved in chatter , data model of chatter and how to query from that objects.In this post i will explain you how to get them on to Vfpage  and chatting from Vfpage.
I am giving code samples,please follow them,by comments you can understand the code.

  Controller:-

public class chatterController {
   public String UserStatus  { get; set; }
   public String profileImageUrl { get; set; 
   public String post { get; set; } 
   public String AddAFeedPost  { get; set; }  
    List<NewsFeed> myfeed = New List<NewsFeed>();
   List<user> lstuser;
   public chatterController ()
   {
        lstuser = [select FullPhotoUrl from User where Id =: UserInfo.getUserId()]; // User profile photo
        profileImageUrl=lstuser[0].FullPhotoUrl;  
   }
    public List<EntitySubscription> GetFollowing()              // Everyone we're following
    {
        List<EntitySubscription> followingES = [
            select id, parentid, subscriberid, parent.name
            from EntitySubscription
            where subscriberid = :UserInfo.getUserId() // user id.
            ];
       return followingES;
    }
    public List<EntitySubscription> GetFollowers()        // All users who are following us
    {   
        List<EntitySubscription> followers = [
            select id, subscriberid, subscriber.name
            from EntitySubscription
            where parentid = :UserInfo.getUserId() // user id.
            ];      
        return followers;
    }
    public void DoUserStatus()
    {
         User user = [select id, CurrentStatus from User where id = :UserInfo.getUserId()];
        user.CurrentStatus = UserStatus;
        update user;
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Status Updated'));
        UserStatus ='';
    }  
    public void AddPost()             // post from vfpage.
    {
          FeedItem fpost = new FeedItem();
            fpost.ParentId = UserInfo.getUserId();
            fpost.Body = AddAFeedPost;
            insert fpost;
         ApexPages.addMessage(new ApexPages.Message(
            ApexPages.Severity.INFO, 'Successfully Posted to Wall'));
        AddAFeedPost = '';
    }
 public List<NewsFeed> NewsFeed()  {                                / Retrieving all feeds form Newsfeed
        List<NewsFeed> myfeed = [SELECT Id, Type, 
                         CreatedById, CreatedBy.FirstName, CreatedBy.LastName,CreatedDate,
                         ParentId, Parent.Name, 
                         Body, Title, LinkUrl, ContentData, ContentFileName,
                             (SELECT Id, FieldName, OldValue, NewValue 
                              FROM FeedTrackedChanges ORDER BY Id DESC), 
                             (SELECT Id, CommentBody, CreatedDate,
                              CreatedBy.FirstName, CreatedBy.LastName
                              FROM FeedComments ORDER BY CreatedDate LIMIT 10),
                             (SELECT CreatedBy.FirstName, CreatedBy.LastName
                              FROM FeedLikes)
                         FROM NewsFeed
                         ORDER BY CreatedDate DESC, Id DESC
                         LIMIT 20];
           return myfeed;
    }

Visualforce page code:-

<apex:page controller="chatterController3" tabStyle="contact" showheader="false">
<Script>
function addComment(recId)
{
alert(recId);
window.showModalDialog('/apex/commentpage?id='+recId ,"dialogWidth:300px; dialogHeight:200px; center:yes");
window.parent.location.reload();
alert(' Comment Added Successfully');
function addLink(recId)
{

alert(recId);
}
</script>
<apex:form >
<apex:pageMessages />
<apex:sectionHeader title="Post to Friends Wall" subtitle="Chatter On Vfpage"/>
<!-- To Post to the wall -->
<apex:pageBlock tabStyle="FEEDItem">
<apex:pageBlockSection title="Post To User Wall">
<table width="150px">
<tr>
<td>
<apex:image id="profileImage" url="{!profileImageUrl}" />
</td>
<td> <apex:outputLabel > Post To Wall:</apex:outputLabel></td><td>
<apex:inputText value="{!AddAFeedPost}" id="status" maxlength="200" size="50" style="padding-bottom: 6px;" />
</td>
<td> <apex:commandButton value="Add A Post" action="{!AddPost}" /> </td>   
</tr>
</table>
</apex:pageBlockSection>
<!-- END-->
<!--User Status Update-->
<apex:pageBlockSection title=" User Status Update" >
<apex:inputText value="{!UserStatus}" id="status" maxlength="600" size="40" style="padding-bottom: 6px;" />
<apex:commandButton value="Update Status" action="{!DoUserStatus}" />  
</apex:pageBlockSection>
<!-- ENd Of User Status Update-->
<!-- List of records,objects,users that the user folowing-->
<apex:pageBlockSection title="User Following" >
<apex:pageBlocktable value="{!Following}" var="f">
<B>  <apex:column value="{!f.parent.name}" headerValue="User Following list" /></B>
</apex:pageBlocktable>
</apex:pageBlockSection>
<!-- End of Followers List-->
<!--Follwers List-->
<apex:pageblockSection title="User Followers">
<apex:pageblockTable value="{!Followers}" var="f">
<apex:column value="{!f.subscriber.name}"  headerValue="Followers List"/>          
</apex:pageblocktable>
</apex:pageblockSection>
<!-- End Of Followers List-->
<!-- Recent Feeds On User Wall-->
<apex:pageblockSection title="Recent Feeds">
<apex:pageBlockTable value="{!NewsFeed}" var="f" style="width:1000px">     
<apex:column width="200" >
<apex:facet name="header">Post By</apex:facet>
<B><apex:outputText value="{!f.Parent.Name}" /></B>
</apex:column>    
<apex:column width="200" >
<apex:facet name="header">Message Body</apex:facet>
<B><apex:outputText value="{!f.Body}" style="font:Oblique"/></B>
</apex:column>  
<apex:column width="200" >
<apex:facet name="header">Created Date</apex:facet>
<B><apex:outputText value=" {0,date,M/d/yyyy h:mm a}">
<apex:param value="{!f.CreatedDate}" />
</apex:outputText></B>
</apex:column> 
<apex:column headerValue="Comments">
<apex:repeat value="{!f.FeedComments}" var="c">
<apex:outputText value="{!c.CreatedBy.FirstName}" id="cfname" style="color:Blue"/>&nbsp;&nbsp;&nbsp;&nbsp; @Said&nbsp;&nbsp;&nbsp;&nbsp;
<apex:outputText value="{!c.CommentBody}" id="comment-repeat"/>
</apex:repeat>
</apex:column> 
<apex:column headerValue="Action" width="200">
<apex:outputlabel value="Comment" onclick="addComment('{!f.id}');" style="color:blue" /> &nbsp;&nbsp;&nbsp;&nbsp;
<apex:outputlabel value="Link" onclick="addLink('{!f.id}');" style="color:Green" />
</apex:column>
</apex:pageBlockTable>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Another Vfpage to Pop up and to add Comment when ever you click on comment:-

<apex:page controller="addComment" showHeader="False">
<script>
function refresh(){
 self.close();
}
</script>
<apex:form >
 <apex:sectionHeader title="Comment" subtitle="Comment To FeedItem"/>
   <apex:pageBlock tabStyle="Contact">
    <apex:pageBlockButtons location="Bottom">
     <apex:commandButton value="Add" Action="{!save}" onclick="refresh()"/>
    </apex:pageBlockButtons>
      <apex:pageBlockSection title="Add COmment">
        <apex:pageBlockSectionItem >
         <apex:outputText ><B>Enter Comment:</B></apex:outputText>
         <apex:inputTextarea value="{!cmttext}" cols="50" rows="5"/>
        </apex:pageBlockSectionItem>
      </apex:pageBlockSection>
     </apex:pageBlock>
     </apex:form>
</apex:page>

Comment Controller:-

public class addComment {
public String cmttext { get; set; }
Public Id cmdid;
public PageReference save() {
FeedComment fcomment = new FeedComment();
cmdid = ApexPages.currentPage().getParameters().get('id');
fcomment.FeedItemId= cmdid;
System.Debug('IDDDDDDDDDDDDDDDDDDDDD'+cmdid);
System.debug('Comment@@@@@@'+cmttext);
fcomment.CommentBody =cmttext;
insert fcomment;
return Null;
}
}
Execute above code.Its perfectly working and you can start chatting from vfpage.
I think this may helps for any one.


No comments:

Post a Comment