如何:获取用户的事件

上次修改时间: 2010年3月18日

适用范围: SharePoint Server 2010

使用 ActivityManager 对象,可以检索两种不同类型的活动事件:当前用户发布的事件和当前用户想要查看的由其他用户发布的事件。某些类型的活动事件的用户首选项存储在 ActivityPreference 对象中。

ActivityPreference 对象

ActivityManager 维护一列 ActivityPreference 对象(在其 ActivityPreferences 属性中为 ActivityPreferencesCollection)。您可以通过使用 ActivityPreferencesCollection 对象的 GetActivityPreferencesPerType() 方法和 SetActivityPreferencesPerType 方法设置和获取用户的活动首选项。本主题假定您已将下列引用添加到 Microsoft Visual Studio 2010 项目。

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • System.Web

获取用户的事件

  • 使用重载的 GetActivitiesByMe() 方法可获取当前用户已发布的事件。

  • 使用重载的 GetActivitiesForMe() 方法可获取当前用户想要查看的由其他用户发布的事件。

  • 使用 ActivityManager 对象的重载的 GetActivitiesByUser 方法可获取指定用户发布的事件。

下面的代码示例演示如何设置当前用户的首选项,然后由当前用户获取活动或获取当前用户的活动。请注意,您需要先访问 ActivityEvent 对象的 LinksList 属性,然后再访问对象的其他属性。

//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);
}

请参阅

引用

Microsoft.Office.Server.ActivityFeed

概念

如何:在用户的新闻流中创建和插入事件

如何:创建新的活动类型

其他资源

Microsoft SharePoint Server 2010:活动源控制台应用程序(该链接可能指向英文页面)

创建资源文件