Tuesday, March 15, 2011

SharePoint News Feed Formatting of ActivityEvent

It is quite easy the get the news feed for activities from your colleagues and for your interests and skills in SharePoint 2010. It is not, however, that simple to format each event to display them in your own web-part using the activity feed object model.

Sure, the data of the different activity types are all there in the ActivityEvent object, and you can get the ActivityTemplate based on the ActivityType of the event. But then you need to process the display template tags to merge in the event values or the event XML from TemplateVariable string property using the SimpleTemplateFormat and ActivityTemplateVariable classes. See the Fun and Games with the ActivityEvent post by Toby Statham to get you started.

Luckily, the activity feed is based on the web syndication model, so you can simply create a SyndicationItem object based on the activity event, and it will find and process the activity template for you:

private Panel CreateFeedEventPanel(ActivityEvent activity)
    Panel panel = new Panel()
CssClass = "MyProfileActivityFeedEventPanel"
    //access the LinkList property in order to populate the ActivityEvent
    List<Link> temp = activity.LinksList;
    string picture = activity.Publisher.Picture;
    picture = string.IsNullOrEmpty(picture) ? "/_layouts/images/O14_person_placeHolder_32.png" : picture;
    Image publisherImage = new Image()
ImageUrl = picture,
AlternateText = activity.Publisher.Name,
CssClass = "MyProfileActivityFeedEventPublisher"
    SyndicationItem syndicationItem = activity.CreateSyndicationItem(_activityManager.ActivityTypes, ContentType.Html);
    panel.Controls.Add(new LiteralControl() { Text = syndicationItem.Summary.Text });
    return panel;
private void PopulateNewsFeedActivityList(bool useTodayOnly)
    string url = SPContext.Current.Site.Url;
    using (SPSite site = new SPSite(url))
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context);
SPUser user = SPContext.Current.Web.CurrentUser;
UserProfile userProfile = profileManager.GetUserProfile(user.LoginName);
_activityManager = new ActivityManager(userProfile, context);
if (useTodayOnly)
    DateTime todayFilter = DateTime.Now.Date;
    _activityList = _activityManager.GetActivitiesForMe(todayFilter);
    _activityList = _activityManager.GetActivitiesForMe(MaxItems);

The formatted HTML will be the same as rendered by the NewsFeedWebPartBase class, except for the profile picture size and some missing timestamps for some event types. Use Reflector on the news feed web-part base class to see the code for mitigating such details.

The code for getting activity events for a user and other SharePoint 2010 social computing "how-tos" can be found in the User Profiles and Social Data section at MSDN.