Showing posts with label Social. Show all posts
Showing posts with label Social. Show all posts

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.

Friday, April 9, 2010

Activity Feed being empty with Claims Enabled Web Applications

I was having trouble aggregating my colleagues' activities into my news feed page. After searching for a solution on the internet, found out that i needed to do two things


  • Add colleagues 
  • Enable the "User - Activity Feed Job" because it was disabled for some reason.

Then when i went to the "My newsfeed" page, i found no feeds still. I didn't get the feeds in my "recent activities" either. After some investigations i found out that the "All Authenticated" users for the Claims provider that the Web Application is using must be added to have read permissions on the social data. You can do this by going to the User Profile Management page and then going to the "Manage User Permissions" page.

After doing this, and then re-running the job. The feeds appeared :)