The Microsoft Dynamics CRM Blog
News and views from the Microsoft Dynamics CRM Team

Working with Activity Feed using Microsoft CRM SDK

Working with Activity Feed using Microsoft CRM SDK

  • Comments 0

Activity Feeds solution is now released in the Dynamics Market Place and ready to use with the latest Microsoft CRM update. You can use our solution out of the box, but  if you want to customize or further integrate with your application, you can use the latest CRM SDK to access the new entities and API for Activity Feeds. That’s what I am going to walk you through in this blog.

Creating an Activity Feed post

If you are already familiar with creating CRM entity instances, creating an Activity Feed post is very simple. You just need to create a Post instance and specify where or which object you want to create your post at (RegardingObjectId) and what do you want to say in your post.

1.       Creating a post in an account record wall

Code snippets:

// Assume account1 is already created

Post account1Post = new Post

{

      RegardingObjectId = account1.ToEntityReference(),

      Text = String.Format("My first Activity Feed post.")

   };

 

 2.       Creating a post in your personal wall (What’s New)

Creating a post in your personal wall is basically creating a post with your user record as the RegardingObjectId.

 

Code snippets:

WhoAmIRequest whoAmIRequest = new WhoAmIRequest();

WhoAmIResponse whoAmIResponse = (WhoAmIResponse)serviceProxy.Execute(whoAmIRequest);

var currentUserRef = new EntityReference(SystemUser.EntityLogicalName, whoAmIResponse.UserId);

// Assume lead1 is already created

Post post2 = new Post

   {

RegardingObjectId = currentUserRef,

Source = new OptionSetValue((int)PostSource.AutoPost),

Text = String.Format("You are now following lead \"{0}\".", lead1.FullName)

   };

 

Notice that the source of this post is defined as an AutoPost. Using the SDK, you can specify the source as AutoPost or ManualPost. If unspecified, source of the post is set to ManualPost. In our Activity Feed solution, the Source attribute is used to specify whether a post is automatically generated by a system (AutoPost) or whether a post is created by a user from the UI.

The difference can be seen on where the posts are shown in the What’s New page. Manual post will be shown under “user posts” section.


Commenting on a post

Creating a comment for a post is very similar to creating a new post. The difference is that you are creating a PostComment instance as opposed to a Post and you are specifying which post you want to comment on (PostId) as opposed to RegardingObjectId

Code snippets:

// Assume account1 is already created

PostComment comment1 = new PostComment

{

PostId = accountPost1.ToEntityReference(),

Text = String.Format("Great progress on this account1!")

};

Following a user or a record in the system

To follow a user or a record in the system, you create a PostFollow instance and you specify the id of the user or record that you want to follow in RegardingObjectId.

Code snippets:

// Assume account1 is already created

PostFollow follow2 = new PostFollow

{

RegardingObjectId = account1.ToEntityReference()

};

Displaying posts and comments in record wall

To display posts and comments in a record wall, you can query them using the regular Retrieve and RetriveMultiple. However, using the new custom SDK messages for Activity Feed, you can retrieve posts and their comments in one single call using RetrieveRecordWall and RetrievePersonalWall request and response.

1.       RetrieveRecordWallRequest returns the list of posts in the specified record wall based on the parameters passed. These are the list of parameters you can specify:

- Entity. The record where the posts resided

- PageSize. How many posts do you want to retrieve at a time

- CommentPerPost. How many comments you want to retrieve in each post. This SDK message will give you an entity collection of post. In each post, you will get the list of Post comments. You can always query all the comments using RetrieveMultiple against the PostId.

- <Optional> Source: do you want to retrieve AutoPost or ManualPost or all of them. By default it will retrieve them all.

- <Optional> StartDate: You specify the StartDate and EndDate if you want to retrieve posts from a specific time range.

- <Optional> EndDate

Code snippets:

// assume lead entity and post are already created in the specific lead record

RetrieveRecordWallRequest retrieveRecordWallReq = new RetrieveRecordWallRequest

{

Entity = lead.ToEntityReference(),

CommentsPerPost = 2,

PageSize = 10,

PageNumber = 1,

Source = new OptionSetValue((int)PostSource.ManualPost)

};

RetrieveRecordWallResponse retrieveRecordWallRes = (RetrieveRecordWallResponse)serviceProxy.Execute(retrieveRecordWallReq);

 foreach (Post post in retrieveRecordWallRes.EntityCollection.Entities)

{

Console.WriteLine("User {0} created post on {1} record wall: {2}", 

post.CreatedBy.Name,

post.RegardingObjectId.Name, post.Text);

} 

The Post entities returned in the EntityCollection are sorted from the most recent to oldest time when the post was modified, such as: the post creation time or the time when a comment is added to the post.

2.       Getting comment count and comments in each post 

In the example above, the CommentsPerPost is set to 2. Therefore, RetrieveRecordWallResponse will only have maximum 2 comments in each post in the entity collection returned. If you want to know the total number of comments in a post, you can check commentcount attribute in each Post.

Code snippets:

// foreach post

AliasedValue commentCountAttr = (AliasedValue)post.Attributes["commentcount"];

int actualCount = (int)commentCountAttr.Value;

Console.WriteLine("\r\n  Comment for this post {0}:", actualCount);

 

       if (post.Post_Comments != null)

       {

              foreach (PostComment comment in post.Post_Comments)

             {

                    Console.WriteLine("Comment: {0}", comment.Text));

              }

       }

Displaying posts that a user is following

To retrieve posts that a user is following (posts that are displayed in the user What’s New page), you can use RetrievePersonalWall SDK message. This message takes similar parameters to RetrieveRecordWall:

- PageSize. How many posts do you want to retrieve at a time

- CommentPerPost. How many comments you want to retrieve in each post. This SDK message will give you an entity collection of post. In each post, you will get the list of Post comments. You can always query all the comments using RetrieveMultiple against the PostId.

- <Optional> Source: do you want to retrieve AutoPost or ManualPost or all of them. By default it will retrieve them all.

- <Optional> StartDate: You specify the StartDate and EndDate if you want to retrieve posts from a specific time range.

- <Optional> EndDate

Code snippets for getting the list of posts in the personal wall (What’s New) and check if there are more posts in the next page:

bool moreRecords = true;

while (moreRecords)

{

RetrievePersonalWallRequest personalWallPageReq = new RetrievePersonalWallRequest

       {

              CommentsPerPost = 2,

              PageNumber = pageNumber,

              PageSize = 5

};

               

RetrievePersonalWallResponse personalWallPageRes = (RetrievePersonalWallResponse)serviceProxy.Execute(personalWallPageReq);

 foreach (Post post in personalWallPageRes.EntityCollection.Entities)

 {

 Console.WriteLine("{0}. Post is created by {1} at {2}",

 post.Text,

 post.CreatedBy.Name,

 post.FormattedValues["createdon"]);

}

 moreRecords = personalWallPageRes.EntityCollection.MoreRecords;

 pageNumber++;

}

 

In both RetrieveRecordWall and RetrievePersonalWall, the Post entities returned are sorted from the most recent to oldest time when the post was modified, such as: the post creation time or the time when a comment is added to the post.

What’s New page vs User record wall

What is the difference between I am calling RetrievePersonalWall and calling RetrieveRecordWall with my User passed as the Entity parameter? From our Activity Feeds solution UI, calling RetrievePersonalWall retrieves all the posts/comments displayed in my What’s New page and RetrieveRecordWall gets all the posts/comments displayed in my User record wall.

 

You may ask what are the differences between what are being displayed in my What’s New wall and my User record wall.

 

My User record wall:

- All the posts and comments that have my User record as the RegardingObjectId

- All the posts where I am being mentioned

- All the posts made by me

 

My What’s New page:

- All the posts and comments that have my User record as the RegardingObjectId

- All the posts where I am being mentioned

- All the posts regarding records I follow

I hope you find this blog useful to help you get started on using the latest SDK to access ActivityFeeds data. Here are the links to the Activity Feed SDK documentation and sample to get you going:

SDK Documentation: Activity Feed Entities

Code sample: Collaborate with Activity Feed

 

Maya Widyasari

Leave a Comment
  • Please add 1 and 2 and type the answer here:
  • Post