コード サンプル: ソーシャル データ統計 Web パーツ

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

適用対象: SharePoint Server 2010

ソーシャル データ統計 Web パーツは、ソーシャル データ統計を表示します。このサンプルは、Microsoft Visual Studio 2010 の SharePoint 可視 Web パーツ プロジェクトです。このプロジェクトを構築して Microsoft SharePoint Server 2010 サイトに展開すると、この Web パーツを、ユーザーのソーシャル タグ活動の統計を表示する任意のページに追加できます。この Web パーツは 3 つのテーブルに以下の情報を表示します。

  • タグ付けされた各 URL、および各 URL のタグ付けに使用された用語

  • ソーシャル タグ内で使用された各用語、およびその用語が使用された回数

  • ソーシャル タグを追加した各ユーザー、およびそのユーザーが URL をタグ付けした回数

このコード サンプルをコンピューターにインストールするには、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 にインストールされます。

オブジェクト モデルを使用して、データの取得と保管を行う

ソーシャル データ統計 Web パーツは、コードに直接記述されたサイトまたはサイト コレクションと共に作成される SPServiceContext オブジェクトを使用して、UserProfileManager オブジェクトと SocialTagManager オブジェクトを作成します。

//Change this value to the root URL for your site.
string socialDataStatsSite = "http://mysite";

using (SPSite siteColl = new SPSite(socialDataStatsSite))
{
//Create SocialTagManager with desired SPServiceContext.
SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);
UserProfileManager myUserProfileManager = new UserProfileManager(serviceContext);
SocialTagManager mySocialTagManager = new SocialTagManager(serviceContext);

ユーザー プロファイル サービス アプリケーションの権限アクセス制御リスト (ACL) に含まれるユーザーだけが、UserProfileManager オブジェクトのインスタンスを作成することができます。ユーザー プロファイル サービス アプリケーションの権限 ACL にユーザーを追加するには、以下の手順を実行します。

ユーザー プロファイル サービス アプリケーションの権限 ACL にユーザーを追加するには

  1. ブラウザーで、SharePoint サーバーの全体管理のホーム ページを開きます。

  2. [アプリケーション構成の管理] 見出しの下に表示される、[サービス アプリケーションの管理] ページに移動します。

  3. [ユーザー プロファイル サービス アプリケーション] オプションを選択します。[権限] をクリックします。

  4. ダイアログ ボックスで、ユーザー アカウントを追加します。[OK] をクリックします。

ソーシャル タグでは有効な分類用語のみが使用されます。したがって、Web パーツが実行する必要のある最初の作業は、現在の分類ストアに作成されているすべてのソーシャル タームを取得することです。各 SocialTerm オブジェクトには、現在の分類ストアからの分類用語、およびソーシャル タグでその用語が使用された回数を表す Count プロパティが含まれます。

//Get all social terms and store in an array.
SocialTerm[] socTerms = mySocialTagManager.GetAllTerms();

次に、Web パーツに表示するすべての情報を格納する Dictionary オブジェクトを作成します。

//Create a Dictionary to store values for tags on social URLs, number of times each social term is used,
//and number of times that each user has tagged a URL.
Dictionary<string, string> SocialUrlStats = new Dictionary<string, string>();
Dictionary<string, long> SocialTermStats = new Dictionary<string, long>();
Dictionary<string, int> SocialTagStats = new Dictionary<string, int>();

Web パーツは、ソーシャル タグで使用されているすべてのソーシャル タームを取得すると、ソーシャル タームの配列を反復処理し、各用語のカウントを格納して、各用語でタグ付けされたすべての URL の取得と格納を行います。

//Get a Taxonomy term for each SocialTerm, get the number of times each social term has been used in a tag,
//and get all the URLs for which that term has been used as a tag. Then map each URL to each of the terms with which
//it has been tagged.
foreach (SocialTerm aTerm in socTerms)
{
  string termName = aTerm.Term.Name;
  SocialTermStats.Add(termName, aTerm.Count);
  try
  {
    SocialUrl[] socURLs = mySocialTagManager.GetAllUrls(aTerm.Term);
    foreach (SocialUrl aUrl in socURLs)
    {
      string urlString = aUrl.Url.ToString();
      if (!SocialUrlStats.ContainsKey(urlString))
      {
        SocialUrlStats.Add(urlString, termName);
      }
      else
      {
        SocialUrlStats[urlString] += ", " + termName;
      }
    }
  }
  catch (UserNotFoundException unfE)
  {
// If the user is not found, handle exception.
  }
}

次に、Web パーツは、UserProfileManager 内の各ユーザーが作成したすべてのソーシャル タグを取得して、SocialTagStatsDictionary オブジェクトにデータを挿入します。その後、各ソーシャル タグをその所有者にマップします。

//Get all the social tags in which the terms have been used.
List<SocialTag> socialTagsList = new List<SocialTag>();
foreach (UserProfile userProf in myUserProfileManager)
{
  try
  {
    SocialTag[] userTags = mySocialTagManager.GetTags(userProf, 0);
    foreach (SocialTag aTag in userTags)
      {
        socialTagsList.Add(aTag);
      }
  }
  catch (UserNotFoundException unfE )
  {
//If the user is not found, handle exception.
  }
}

//For each SocialTag, get the owner and get the number of times that owner has tagged a URL.
foreach (SocialTag aTag in socialTagsList)
{
  if (aTag != null)
  {
    if (!SocialTagStats.ContainsKey(aTag.Owner.DisplayName))
    {
      int tagCount = mySocialTagManager.GetCount(aTag.Owner);
      SocialTagStats.Add(aTag.Owner.DisplayName, tagCount);
    }
  }
}

最後に、Web パーツは、各 Dictionary オブジェクトを GridView オブジェクトにバインドして、取得したソーシャル データの統計を表示します。ユーザー インターフェイスで使用された文字列は、リソース ファイルに格納されます。

//Bind each Dictionary to a GridView control. Display localized header strings for each column.
GridView1.DataSource = SocialUrlStats;
GridView1.Columns[0].HeaderText = Properties.Resources.String1;
GridView1.Columns[1].HeaderText = Properties.Resources.String2;
GridView1.DataBind();
GridView2.DataSource = SocialTermStats;
GridView2.Columns[0].HeaderText = Properties.Resources.String3;
GridView2.Columns[1].HeaderText = Properties.Resources.String4;
GridView2.DataBind();
GridView3.DataSource = SocialTagStats;
GridView3.Columns[0].HeaderText = Properties.Resources.String5;
GridView3.Columns[1].HeaderText = Properties.Resources.String2;
GridView3.DataBind();
}

サンプルのビルドと実行

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

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

  1. Microsoft.SDK.Server.Samples という名前のフォルダーを作成し、SocialDataStatistics.zip ファイルをそのフォルダーで解凍します。

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

  3. [プロパティ] ウィンドウで、開発またはテスト サイトの絶対アドレスのサイト URL 値を指定します (http://mysite/ など。末尾のスラッシュを必ず含めます。また、この URL を、VisualWebPart1UserControl.ascx.cs ファイル内にある socialDataStatsSite 文字列の値にします)。

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

    • Microsoft.SharePoint.dll

    • Microsoft.SharePoint.Taxonomy.dll

    • Microsoft.Office.Server.dll

    • Microsoft.Office.Server.UserProfiles.dll

  5. [ビルド] メニューの [ソリューションの展開] を選択します。ビルドが完了すると、Web パーツが開発またはテスト サイトにインストールされます。

サンプルを実行するには

  • 新しくインストールした Web パーツを、サイトの任意のページに追加します。[ユーザー設定] カテゴリから、[VisualWebPartProject1] Web パーツを選択します。

    ヒントヒント

    この Web パーツのコントロールが見つからないことを示すエラーがページに表示される場合は、\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES に移動して、SocialDataStatisticsWebPart の名前を VisualWebPartProject1 に変更します。

関連項目

概念

オブジェクト モデルからのソーシャル データの作成と使用

その他の技術情報

Code Gallery (英語)