Friday, August 16, 2013

Get Social tags in SharePoint 2010

Social Tagging in SharePoint really helps in categorizing the information upon User’s interest with meaningful tag and also improves the quality of search and sharing the data with others.

SocailTagManager is a sealed class which contains the methods and properties used to handle social Tag data.
We are going to use GetTags method from SocialTagManager class which returns all the Tags based on the Item URL or User Profile. To know all the methods and preoperties of SocialTagManager class click here.
(Check inline comments for more details).
var type = typeof(SocialTagManager);
var methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Instance);
// Get the method from SocialTagManager sealed class which returns all the Tags
var method = methods.FirstOrDefault(m => m.ToString() == "Microsoft.Office.Server.SocialData.SocialTag[] GetTags(System.Uri, Int32, Microsoft.Office.Server.SocialData.SocialItemPrivacy)");
if (method != null)
SPServiceContext serviceContext = SPServiceContext.GetContext(ospWeb.Site);
SocialTagManager stm = new SocialTagManager(serviceContext);
// Get page URL
Uri uri = new Uri(listItemURL);
// Retrieve only the public tags
var itemTags = (SocialTag[])method.Invoke(stm, new object[] { uri, 1000, SocialItemPrivacy.PublicOnly });
// Get the count of the selected tag.
likeCount = itemTags.Count(t => t.Term.Name.ToLower() == "i like it");
// Filter the tags with the selected tag
var socialTags = itemTags.Where(t => t.Term.Name.ToLower() == "i like it");
string username ="";
foreach (var socialTag in socialTags)
    // Each tag will be mapped with the Profile ID
    var user = socialTag.Owner;
    string personalSite = user.PublicUrl.AbsoluteUri;   
    username = username + ";" + user.DisplayName;

Related Posts Plugin for WordPress, Blogger...