Friday, January 21, 2011

Retrieve Activities from the SharePoint 2010 Activity Feeds

I've recently worked on a webpart that will let you retrieve activities for the logged in user and display them using the default template of the activity.

The webpart gets an instance of the ActivityManager object and the UserProfile object of the logged in user. After you get the reference, you can then retrieve the activity events using the ActivityManager.GetActivitiesByUser() method. Then you retrieve the resource (.resx) file corresponding to that particular ActivityEvent replacing the values with the ActivityEvent values as shown the code below

            string userName = Environment.UserDomainName + "//" + Environment.UserName;
            SPServiceContext currentContext = SPServiceContext.GetContext(SPContext.Current.Site);

            //Get the UserProfileManager from SPServiceContext.
            UserProfileManager userProfMan = new UserProfileManager(currentContext);

            //Get the current user.
            UserProfile currentUser = userProfMan.GetUserProfile(userName);

            //Get the ActivityManager from the user and context.
            ActivityManager activityMan = new ActivityManager(currentUser, currentContext);

            ActivityEventsCollection eventsCollection = activityMan.GetActivitiesForMe(10);

            foreach (ActivityEvent activity in eventsCollection)
                ActivityType activityType = activityMan.ActivityTypes[activity.ActivityTypeId];
                ActivityTemplate activityTemplate = activityType.ActivityTemplates[bool.FalseString];
                string templateStr = SPUtility.GetLocalizedString(activityTemplate.TitleFormatLocStringResourceFile, "$Resource:" + activityTemplate.TitleFormatLocStringName, (uint)CultureInfo.CurrentUICulture.LCID);
                //The line below returns null for the "Publisher".
                //HOWEVER, If i step through the code in debug mode, the values get populated eventually
                templateStr = templateStr.Replace("{Publisher}", activity.Publisher.Name);

                //Then you will need to output the templateStr to the UI.
                //EncodedLiteral.Text += templateStr

What you might notice is that you will get an "Null" Exception when you query the Publisher and the Owner properties of the ActivityEvent object. Stepping through the code, you will notice that the values will eventually load, and you will not get the exception. I'm not sure why this happens. If anyone knows, Feel free to leave a comment. A solution for this would be to get the TemplateVariable property and parse it as an XmlDocument object and then use that to get the Event values.

1 comment:

  1. string templateStr = SPUtility.GetLocalizedString(activityTemplate.TitleFormatLocStringResourceFile, "$Resource:" + activityTemplate.TitleFormatLocStringName, (uint)CultureInfo.CurrentUICulture.LCID);

    this your code line is wrong sequence and "$Resources:" have "s" too.

    correct code is below:
    var templateStr = SPUtility.GetLocalizedString("$Resources:" + activityTemplate.TitleFormatLocStringName, activityTemplate.TitleFormatLocStringResourceFile, (uint)CultureInfo.CurrentUICulture.LCID);

    Enjoy :)