ユーザー別のイベントを取得する

最終更新日: 2010年3月18日

適用対象: SharePoint Server 2010

ActivityManager オブジェクトを使用すると、現在のユーザーが発行したイベントと、他のユーザーが発行したイベントのうち現在のユーザーが参照するイベント、これらの 2 種類のイベント アクティビティを取得できます。特定の種類のイベント アクティビティに関するユーザー設定は、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: Activity Feeds Console Application (英語)

リソース ファイルの作成