다음을 통해 공유


How to: Get Events for Users

Applies to: SharePoint Server 2010

The ActivityManager object enables you to retrieve two different kinds of activity events: events published by the current user, and events published by other users that the current user wants to see. User preferences for certain types of activity events are stored in ActivityPreference objects.

ActivityPreference Objects

The ActivityManager maintains a list of ActivityPreference objects (in its ActivityPreferences property, which is an ActivityPreferencesCollection). You can set and get the activity preferences for a user by using the GetActivityPreferencesPerType() method and the SetActivityPreferencesPerType method of the ActivityPreferencesCollection object. This topic assumes that you have added the following references to your Microsoft Visual Studio 2010 project.

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • System.Web

Getting Events for Users

  • Use the overloaded GetActivitiesByMe() method to get events that the current user has published.

  • Use the overloaded GetActivitiesForMe() method to get events published by other users that the current user wants to see.

  • Use the overloaded GetActivitiesByUser method of the ActivityManager object to get events published by specified users.

The following code sample demonstrates how to set preferences for the current user, and then get activities by and for the current user. Note that you need to access the LinksList property of the ActivityEvent objects before accessing their other properties.

//Get the desired site context.
string currentSite = "site url";
using (SPSite aSite = new SPSite(currentSite))
{
SPServiceContext currentContext = SPServiceContext.GetContext(aSite);
//Get the UserProfileManager from SPServiceContext.
UserProfileManager userProfMan = new UserProfileManager(currentContext);
//Get the current user.
string userName = Environment.UserDomainName + "\\" + Environment.UserName;
UserProfile currentUser = userProfMan.GetUserProfile(userName);
//Get the ActivityManager from the user and context.
ActivityManager activityMan = new ActivityManager(currentUser, currentContext);
//Create an instance of a list of ActivityPreferencePerType objects.
List<ActivityPreferencePerType> activityPrefsPerType = new List<ActivityPreferencePerType>(activityMan.ActivityTypes.Count);
 
//Get each ActivityType stored in ActivityManager, and for testing purposes, set each ActivityType as
//a "true" ActivityPreference.
foreach (ActivityType activityType in activityMan.ActivityTypes)
{
ActivityPreferencePerType newPref = new ActivityPreferencePerType();
newPref.ActivityType = activityType;
newPref.IsSet = true;
activityPrefsPerType.Add(newPref);
Console.WriteLine(activityType.ActivityTypeName + " " + activityType.ActivityTypeId);
}
//Set activity preferences for the user.
activityMan.ActivityPreferences.SetActivityPreferencesPerType(activityPrefsPerType);
//Get all activity events for the user. You can also use GetActivitiesByUser and 
// pass currentUser as an argument.
ActivityEventsCollection activityEventsByMeCollection = activityMan.GetActivitiesByMe();
ActivityEventsCollection activityEventsForMeCollection = activityMan.GetActivitiesForMe();
//Iterate through one of the collections to verify.
foreach (ActivityEvent activityEvent in activityEventsForMeCollection)
{
//Access the LinkList property in order to populate the ActivityEvent
//object properties
List<Link> temp = activity.LinksList;
Console.WriteLine(activityEvent.Publisher.Name);
}
Console.ReadKey(true);
}

See Also

Reference

Microsoft.Office.Server.ActivityFeed

Concepts

How to: Create and Insert Events on a User's Newsfeed

How to: Create a New Activity Type

Other Resources

Microsoft SharePoint Server 2010: Activity Feeds Console Application

Creating Resource Files