次の方法で共有


コード サンプル: 仕事仲間アクティビティ イベントへのリンクを送る

最終更新日: 2011年8月22日

適用対象: SharePoint Server 2010

この記事の内容
SharePoint アクティビティの概要
アクティビティ イベントの作成と発行
サンプルのビルドと実行

このサンプルでは、ActivityType オブジェクトと ActivityEvent オブジェクトを作成する方法、および新しい ActivityEvent オブジェクトをユーザーのニュースフィードに挿入する方法を示します。このサンプルでは、ユーザーがいくつかの簡単な手順を実行するだけで仕事仲間とリンクを共有できるようにするための簡単なフォームを作成します。ユーザー インターフェイスには、ユーザーが仕事仲間のニュースフィードに URL とコメントを挿入するために使用できるポップアップ ボックスがあります。

サンプル コードの提供者:MVP コントリビューター Mathew McDermott、Catapult Systems | MVP コントリビューター Andrew Connell、Critical Path Training, LLC

このコード サンプルをコンピューターにインストールするには、Microsoft SharePoint 2010 Software Development Kit (英語) (SDK) をダウンロードするか、Code Gallery (英語) からサンプルをダウンロードします。SharePoint 2010 SDK をダウンロードすると、サンプルはファイル システムの次の場所にインストールされます。C:\Program Files\Microsoft SDKs\SharePoint 2010\Samples\Social Data and User Profiles。

SharePoint アクティビティの概要

Microsoft SharePoint Server 2010 では、個人用サイトのホストのニュースフィードによって、監視対象のイベントと通知を統合できます。この新しいページと新しい機能によってユーザーは、企業中の更新を 1 つの場所で確認できます。ユーザーは、ユーザーの画像と関連情報へのリンクで書式設定された、ニュースと情報のわかりやすい表示に "参加" できます。これにより、ニュースフィードは、追加のアプリケーション通知に便利な場所になります。各ニュースフィード項目は、ActivityEvent オブジェクトの内容を表示します。

アクティビティのテンプレート

アクティビティの設計は、簡単な文から始めます。たとえば、仕事仲間がリンクの共有を望んでいることを表す新しい ActivityType オブジェクトを作成する場合、文は次のようになります。

Jim Brown has sent you the following link of interest: Great SharePoint Article

アクティビティの可変部分は Jim Brown で、リンク テキストは Great SharePoint Article です。アクティビティ タイプのテンプレートの開発では、次のサンプル テンプレートのように可変部分をトークンに置き換えます。

{Publisher} has sent you the following link of interest: {Link}

開発するカスタム アプリケーションによって、トークンに必要な情報を入力します。テンプレートのテキストは、対象の言語へのローカライズを容易にするために、リソース ファイルとしてサーバーに展開されます。リソース ファイルの作成と使用の詳細については、「リソース ファイルの操作 (英語)」を参照してください。

このソリューションの展開

表 1 に、このアクティビティ フィード ソリューションに必要なコンポーネントを示します。

表 1. アクティビティ フィード ソリューションに必要なコンポーネント

プロジェクト アセット

展開先

プロジェクト アセンブリ (*.dll)

グローバル アセンブリ キャッシュ

アクティビティ テンプレートのリソース ファイル (*.resx)

Resources ディレクトリ

アプリケーション ページ (*.aspx)

Template/Layouts/MSDN

アクティビティ アセットを展開した後で、新しい ActivityType オブジェクトと関連する ActivityTemplate オブジェクトを、ActivityType オブジェクトと ActivityTemplate オブジェクトをリソース ファイルと関連付けるイベント レシーバーによって作成する必要があります。サンプル プロジェクトでは、ソリューションの展開時にこのアクションを実行します。

private void SetupCustomActivity(SPSite site)
{
  try
  {
    //Get a Service Context from the site.
    SPServiceContext context = SPServiceContext.GetContext(site);
    //We need a User Profile Manager to work with Activities.
    UserProfileManager upm = new UserProfileManager(context);
    //Grab the current user's profile.
    UserProfile p = upm.GetUserProfile(true);

    Debug.WriteLine("Got the User Profile for: " + p.DisplayName);
    //Grab an activity manager.
    ActivityManager am = new ActivityManager(p, context);
    //Ensure that we have permission to do this.
    bool hasrights = am.PrepareToAllowSchemaChanges();
    Debug.WriteLine("Does installer have Admin rights to change the schema? " + hasrights);

    // Create our activity application.
    Debug.WriteLine("Create our Activity Application");
    ActivityApplication app = am.ActivityApplications["Send Link to Colleague"];
    if (app == null)
    {
      app = am.ActivityApplications.Create("Send Link to Colleague");
      app.Commit();
    }
    Debug.WriteLine("Send Link to Colleague App id: " + app.ApplicationId);

    //Create our Colleagues Activity Type, but first check to see whether it already exists.
    Debug.WriteLine("Activity Type: SendColleagueUrlNote");
    ActivityType activityType = app.ActivityTypes["SendColleagueUrlNote"];
    if (activityType == null)
    {
      activityType = app.ActivityTypes.Create("SendColleagueUrlNote");
      activityType.ActivityTypeNameLocStringResourceFile = resFile;
      activityType.ActivityTypeNameLocStringName = "ActivityFeed_SendColleagueUrlNote_Type_Display";
      activityType.IsPublished = true;
      activityType.IsConsolidated = true;
      activityType.Commit();
    }
    //SendColleagueUrlNote Activity Template.
    Debug.WriteLine("SendColleagueUrlNote Activity Single Value Template");
    ActivityTemplate urlTemplateSV = activityType.ActivityTemplates[ActivityTemplatesCollection.CreateKey(false)];
    if (urlTemplateSV == null)
    {
      urlTemplateSV = activityType.ActivityTemplates.Create(false);
      urlTemplateSV.TitleFormatLocStringResourceFile = resFile;
      urlTemplateSV.TitleFormatLocStringName = "ActivityFeed_SendColleagueUrlNote_SV_Template";
      urlTemplateSV.Commit();
     }

アクティビティ イベントの作成と発行

サンプル アプリケーションの設計目標は、いくつかの簡単な手順を実行するだけで仕事仲間とリンクを共有できる簡単なフォームをユーザーに提供することです。タグとメモ機能をモデルとした、"仕事仲間へリンクを送信" 機能によって、図 1 のように個人用メニューに 1 つのメニュー項目が追加されます。

図 1. [仕事仲間へリンクを送信] メニューの詳細

[仕事仲間へリンクを送信] メニューの詳細

このメニュー項目をユーザーが選択すると、[仕事仲間へリンクを送信] フォームが表示されます。

図 2. [仕事仲間へリンクを送信] ページ

[仕事仲間へリンクを送信] ページ

フォームを開始するために使用される ECMAScript (JavaScript、JScript) は、現在のウィンドウの URL を提供し、URL をパラメーターとしてフォームに渡します。渡される値は、フォームのデバッグ セクションに表示されます。カスタム アクティビティを仕事仲間に送信するには、ユーザーは、テキスト ボックスにメモを追加して、[Post] をクリックします。

アクティビティの作成

サンプル アプリケーションでは、CreateActivity メソッドでユーザー用のアクティビティを作成します。このメソッドは、アクティビティ所有者のユーザー プロファイルとアクティビティ発行者のユーザー プロファイルを引数として取ります。このメソッドは、各ユーザーの MinimalPerson オブジェクトを作成し、これらのオブジェクト両方を CreateActivityEvent メソッドに渡します。アクティビティ リンクは、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 some properties.
  ActivityManager am = new ActivityManager();
  ActivityEvent activityEvent = ActivityEvent.CreateActivityEvent(am, activityType.ActivityTypeId, ownerMP, publisherMP);

  Debug.WriteLine("Got my ActivityEvent");
  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();

  Debug.WriteLine("End of Activity event: " + activityType.ActivityTypeName);
  return activityEvent;
}

ユーザーの仕事仲間へのアクティビティ イベントの送信

このソリューションでは、ActivityFeedGatherer オブジェクトによって提供されるバッチ メソッドを使用して、アクティビティをユーザーの仕事仲間全員にマルチキャストします。

//Send activity to the Owner’s Colleagues.
//Create Dictionary objects that will be output parameters for the batch gatherer methods.
  Dictionary<long, MinimalPerson> owners;
  Dictionary<long, List<MinimalPerson>> colleaguesOfOwners;
//Create List objects that contain the ActivityEvent and its publisher.
  List<ActivityEvent> events = new List<ActivityEvent>();
  events.Add(activityEvent);
  List<long> publishers = new List<long>();
  publishers.Add(activityEvent.Owner.Id);
  ActivityFeedGatherer.GetUsersColleaguesAndRights(activityManager, publishers, out owners, out colleaguesOfOwners);
  Dictionary<long, List<ActivityEvent>> eventsConsolidatedPerOwner;
  ActivityFeedGatherer.MulticastActivityEvents(activityManager, events, colleaguesOfOwners, out eventsConsolidatedPerOwner);
  List<ActivityEvent> eventsConsolidated;
  ActivityFeedGatherer.CollectActivityEventsToConsolidate(eventsConsolidatedPerOwner, out eventsConsolidated);
  ActivityFeedGatherer.BatchWriteActivityEvents(eventsConsolidated, 0, events.Count);

サンプルのビルドと実行

以下の手順では、開発サイトあるいはテスト サイトで、このプロジェクトをテストする方法を説明します。

サンプルをビルドするには

  1. 「Microsoft.SDK.Server.Samples」という名前のフォルダーを作成し、MSDN SharePoint 2010 Activity Feeds - Code.zip ファイルをそのフォルダーに解凍します。

  2. Visual Studio 2010 を起動し、手順 1. で作成したフォルダーにある SendColleagueURLNote.sln ファイルを開きます。

  3. [プロパティ] ウィンドウで、開発サイトまたはテスト サイトのサイト URL の値を絶対アドレス (たとえば、http://mysite/) で指定します。終了スラッシュを入力していることを確認してください。

  4. SendColleagueURLNote.EventReceiver.cs ファイルで、SendColleagueURLNoteEventReceiver メソッドで SharePoint サーバーの全体管理サイトの URL を指定します。

  5. もし存在しない場合は、プロジェクトに以下のアセンブリへの参照を追加します。

    • Microsoft.SharePoint.dll

    • Microsoft.Office.Server.dll

    • Microsoft.Office.Server.UserProfiles.dll

  6. Resources ディレクトリのリソース ファイル (URLNote.en-US.resxURLNote.resx) を \Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Resources ディレクトリに追加します。

  7. [ビルド] メニューの [ソリューションの展開] をクリックします。ビルドが完了した後で、アプリケーション ページは開発あるいはテスト サイトにインストールされます。

サンプルを実行するには

  1. ソリューションをビルドし、展開した後で、SharePoint Server 2010 サイトに移動します。ページの右上にあるドロップダウン メニューで [仕事仲間へリンクを送信] をクリックします。

  2. テキスト ボックスにオプションのメモを入力し、[Post] をクリックしてそのメモとリンクを仕事仲間のニュースフィードに挿入します。

関連項目

参照

Microsoft.Office.Server.ActivityFeed

概念

オブジェクト モデルからのアクティビティ フィードの使用