ソーシャル タグを作成および取得する
最終更新日: 2010年3月30日
適用対象: SharePoint Server 2010
SocialTagManager オブジェクトを使用すると、使用している分類からの有効な Term で、指定した URL に対するソーシャル タグを作成できます。ここでは、SocialTagManager を使用してカスタム アプリケーションでソーシャル タグを作成および取得する方法を示します。このトピックに含まれるタスクを実行する方法を示すコード例については、「コード サンプル: ソーシャル データ統計 Web パーツ」と「コード サンプル: 仕事仲間承認ソーシャル タグ付けアプリケーション ページ」を参照してください。このトピックの例では、次の参照を Microsoft Visual Studio 2010 プロジェクトに追加してあるものとします。
Microsoft.SharePoint
Microsoft.Office.Server
Microsoft.Office.Server.UserProfiles
System.Web
ソーシャル タグの作成
オーバーロードされた AddTag メソッドは、指定された単一の URL で現在のユーザーに対する単一の SocialTag オブジェクトを作成します。ソーシャル タグは、URL と、使用している分類からの Term で構成されます。分類 Term が存在しない場合、用語ストアに追加する必要があります。SocialTag は、必要に応じて、タグの対象の URL で表されるオブジェクトの文字列タイトルと、ソーシャル タグがプライベートで現在のユーザーに対してのみ表示されるかどうかを指定するブール値によって構成することもできます。IsPrivate ブール値プロパティの既定の設定は false なので、ソーシャル タグは既定ではパブリックです。次のサンプルでは、用語を既定のキーワード用語ストアに追加した後、SocialTagManager を使用してその用語を単一のタグとして指定した URL に追加する方法を示します。
Uri myUri = new Uri("URL");
using (SPSite site = new SPSite("SharePoint site URL"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
SocialTagManager mySocialTagManager = new SocialTagManager(context);
//Retrieve the taxonomy session from the SocialTagManager.
TaxonomySession taxSession = mySocialTagManager.TaxonomySession;
TermStore termStore = taxSession.DefaultKeywordsTermStore;
myTerm = termStore.KeywordsTermSet.CreateTerm("term", termStore.DefaultLanguage);
termStore.CommitAll();
mySocialTagManager.AddTag(myUri, myTerm);
}
ソーシャル タグの取得
オーバーロードされた GetTags メソッドは、指定された URL またはユーザーに対するすべての SocialTag オブジェクトを取得します。URL を指定すると、メソッドは現在の SPServerContext で現在のユーザーによってその URL に追加されたすべてのソーシャル タグを返します。ユーザーを指定すると、メソッドは指定したユーザーが追加したすべてのソーシャル タグを返します。
using (SPSite site = new SPSite("SharePoint site URL"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileManager myUserProfileManager = new UserProfileManager(context);
UserProfile myUserProfile = myUserProfileManager.GetUserProfile(false);
SocialTagManager mySocialTagManager = new SocialTagManager(context);
SocialTag[] tags = mySocialTagManager.GetTags(myUserProfile);
Console.WriteLine("Tags for user:");
foreach (SocialTag tag in tags)
{
Console.WriteLine(tag.Term.Name + ": " + tag.Uri.AbsoluteUri);
}
}
関連項目
参照
Microsoft.Office.Server.SocialData