ユーザーのニュースフィードにイベントを作成して挿入する

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

適用対象: SharePoint Server 2010

Microsoft SharePoint Server 2010 では、イベントを作成して 1 人のユーザーのニュースフィードに挿入したり、イベントを複数のユーザーのニュースフィードにマルチキャストすることにより、個人用サイト ホストの [個人用ニュースフィード] ページを拡張できます。以下のサンプルでの操作はいずれも同じパターンに従います。所有者と発行者が同じユーザーであるアクティビティを作成してから、イベントを他のユーザーのニュースフィードに挿入するために、イベントをコピーし、元のユーザーの仕事仲間をそのコピーの所有者として指定します。このトピックに含まれるタスクの実行方法を示すコード サンプルについては、「コード サンプル: マルチキャスト アクティビティ イベント コンソール アプリケーション」と「コード サンプル: 仕事仲間アクティビティ イベントへのリンクを送る」を参照してください。ここでは、次の参照が Microsoft Visual Studio 2010 プロジェクトに追加されていることを前提とします。

重要重要

ユーザーのプライバシーを保護するため、アクティビティ イベントを作成してユーザーのニュースフィードに挿入するには User Profile Service アプリケーションの管理者権限が必要です。

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • System.Web

ユーザーのニュースフィードにイベントを作成して挿入する

次のサンプル メソッドでは、URL と説明のテキストをすべての仕事仲間に送信するユーザーの操作を表す仮定の ActivityType 向けに ActivityEvent オブジェクトを作成します。このメソッドは、イベントの所有者と発行者を表す UserProfile オブジェクト、新しい ActivityEvent を作成するために ActivityManager で使用できる ActivityType オブジェクト、およびイベントに関連付けられる URL と説明のテキストを表す文字列を受け取ります。

public ActivityEvent CreateActivity(UserProfile ownerProfile, UserProfile publisherProfile, ActivityType activityType, string linkText, string linkUrl)
{
  //Get the owner and publisher profile.
  Entity ownerMP = new MinimalPerson(ownerProfile).CreateEntity(activityManager);
  Entity publisherMP = new MinimalPerson(publisherProfile).CreateEntity(activityManager);

  //Create the activity and set properties.
  ActivityManager am = new ActivityManager();
  ActivityEvent activityEvent = ActivityEvent.CreateActivityEvent(am, activityType.ActivityTypeId, ownerMP, publisherMP);

  activityEvent.Name = activityType.ActivityTypeName;
  activityEvent.ItemPrivacy = (int)Privacy.Public;
  activityEvent.Owner = ownerMP;
  activityEvent.Publisher = publisherMP;

  //Create the link for the activity.
  Link link = new Link();
  link.Href = linkUrl;
  link.Name = linkText;
  activityEvent.Link = link;
  //Save your work.
  activityEvent.Commit();

  return activityEvent;
}

この CreateActivity メソッドでは、所有者と発行者が同じユーザーとして識別される元の ActivityEvent を作成できます。また、同じイベントを他のユーザーのニュースフィードに挿入することもできます。以下のサンプルでは、ユーザーの仕事仲間のリストを反復処理し、各仕事仲間が所有者として識別されるアクティビティを作成します。

foreach (Colleague colleague in publisher.Colleagues.GetItems())
{
  //Set the owner of the activity to the colleague.
  CreateActivity(colleague.Profile, publisher, activityType, txtNote.Text, strUrl);
}

ユーザーのニュースフィードにイベントをマルチキャストする

ActivityFeedGatherer クラスを使用して、複数のユーザーにイベントをマルチキャストすることができます。ActivityFeedGatherer クラスのメソッドは、複数の発行者からイベントを複数のユーザーのニュースフィードに挿入する必要がある場合に特に便利です。また、サイトのパフォーマンスが重視されるときにこの方法を使用することをお勧めします。

次のサンプル メソッドでは、ActivityFeedGatherer クラスの 4 つの主要なメソッドを使用しています。

GetUsersColleaguesAndRights メソッドは、ActivityEvent オブジェクトのリストから所有者の識別子のリストを受け取り、出力パラメーターとして渡される 2 つの Dictionary オブジェクトにそれらを設定します。最初の Dictionary オブジェクトは、所有者の識別子をその所有者を表す MinimalPerson オブジェクトにマップします。2 番目の Dictionary オブジェクトは、各所有者の識別子をその所有者の仕事仲間を表す MinimalPerson オブジェクトのリストにマップします。

ActivityFeedGatherer.GetUsersColleaguesAndRights(m_ActivityManager, publishers, out owners, out colleaguesOfOwners);

MulticastActivityEvents メソッドは、ActivityEvent オブジェクトのリストを受け取り、2 番目の Dictionary から返される仕事仲間にそれらを配布します。この操作を行うために、各 ActivityEvent オブジェクトのコピーを作成し、各仕事仲間をそのコピーの所有者にします。また、MulticastActivityEvents メソッドは、出力パラメーターとして渡される別の Dictionary オブジェクトにも値を設定します。この Dictionary は、新しい所有者を新しくコピーした ActivityEvent オブジェクトのリストにマップします。

ActivityFeedGatherer.MulticastActivityEvents(m_ActivityManager, m_ActivityEvents, colleaguesOfOwners, out eventsPerOwner);

これらの新しいイベントは、データベースにバッチ処理で書き込む必要があります。CollectActivityEventsToConsolidate メソッドは、この eventsPerOwner ディクショナリをイベントのフラット リストに変換して、BatchWriteActivityEvents メソッドに渡せるようにするヘルパー メソッドです。

ActivityFeedGatherer.CollectActivityEventsToConsolidate(eventsPerOwner, out eventsToWrite);
ActivityFeedGatherer.BatchWriteActivityEvents(eventsToWrite, startIndex, m_ActivityManager.MaxEventsPerBatch);

以下のサンプル メソッドは、これらのすべての ActivityFeedGatherer メソッドを組み合わせたものです。また、データベースに書き込む必要がある ActivityEvent オブジェクトのリストを反復処理して、ActivityManager オブジェクトの MaxEventsPerBatch プロパティの値を超えないようにします。

public void MulticastPublishedEvents(List<ActivityEvent> m_ActivityEvents, ActivityManager m_ActivityManager)
{
   if (m_ActivityEvents.Count == 0)
   return;

   List<long> publishers = new List<long>();

//Populate the list of publishers.
   foreach (ActivityEvent activityEvent in m_ActivityEvents)
   {
      if (!publishers.Contains(activityEvent.Owner.Id))
      publishers.Add(activityEvent.Owner.Id);
   }

//Create the Dictionary objects that will contain the owners and colleagues.
   Dictionary<long, MinimalPerson> owners;
   Dictionary<long, List<MinimalPerson>> colleaguesOfOwners;
   Dictionary<long, List<ActivityEvent>> eventsPerOwner;

//Get the colleagues of the ActivityEvent publishers.
   ActivityFeedGatherer.GetUsersColleaguesAndRights(m_ActivityManager, publishers, out owners, out colleaguesOfOwners);

//Multicast the events.
   ActivityFeedGatherer.MulticastActivityEvents(m_ActivityManager, m_ActivityEvents, colleaguesOfOwners, out eventsPerOwner);

//Create a list of events that need to be written to the database.
   List<ActivityEvent> eventsToWrite;
   ActivityFeedGatherer.CollectActivityEventsToConsolidate(eventsPerOwner, out eventsToWrite);

//Iterate through the eventsToWrite list to ensure that you do not exceed
//the value of the MaxEventsPerBatch property.
   int startIndex = 0;
   while (startIndex + m_ActivityManager.MaxEventsPerBatch < eventsToWrite.Count)
   {
      ActivityFeedGatherer.BatchWriteActivityEvents(eventsToWrite, startIndex, m_ActivityManager.MaxEventsPerBatch);
      startIndex += m_ActivityManager.MaxEventsPerBatch;
   }
   ActivityFeedGatherer.BatchWriteActivityEvents(eventsToWrite, startIndex, eventsToWrite.Count - startIndex);
}

関連項目

参照

Microsoft.Office.Server.ActivityFeed

概念

[方法] ユーザー別のイベントを取得する

[方法] 新しいアクティビティ タイプを作成する

その他の技術情報

Microsoft SharePoint Server 2010: Activity Feeds Console Application (英語)

リソース ファイルの作成 (英語)