Export (0) Print
Expand All

How to: Create and Retrieve a Social Tag

SharePoint 2010

Last modified: March 30, 2010

Applies to: SharePoint Server 2010

The SocialTagManager object enables you to create a social tag for any specified URL with a valid Term from the taxonomy that you are using. This topic demonstrates how to use the SocialTagManager to create and retrieve social tags in a custom application. See Code Sample: Social Data Statistics Web Part and Code Sample: Colleague Approval Social Tagging Application Page for code samples that demonstrate how to perform tasks that are included in this topic. The examples in this topic assume that you have added the following references to your Microsoft Visual Studio 2010 project:

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • System.Web

The overloaded AddTag method creates a single SocialTag object for the current user on a single specified URL. A social tag consists of a URL and a Term from the taxonomy that you are using. If the taxonomy Term does not exist, you must add it to the term store. A SocialTag also can optionally consist of a string title for the object represented by the URL being tagged and a Boolean value that determines whether the social tag is private and visible only to the current user. The default setting for the IsPrivate Boolean property is false, so social tags are public by default. The sample below demonstrates how to add a term to the default keywords term store and then use the SocialTagManager to add that term as a single tag to a specified 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);
}

The overloaded GetTags method retrieves all SocialTag objects for a specified URL or user. If you specify a URL, the method returns all social tags added to that URL by the current user in the current SPServerContext. If you specify a user, the method returns all social tags that the specified user added.

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);
}
}
Show:
© 2015 Microsoft