Programmatically get all the Social Tags for the Specified User in SharePoint 2010


Social Tag can be created for any specified URL with a valid term using SocialTagManager object. SocialTagManager object contains methods and properties that are used to manipulate social tag data. Here we will be seeing how to get all the social tags for the specified URL in SharePoint 2010 using the SharePoint Object Model.

View all the social tags for the specified user through Central Administration:

  1. Open Central Administration by going Start | All Programs | Microsoft SharePoint 2010 Products | SharePoint 2010 Central Administration.
  2. Click on Manage Service Application which is available in Application Management section.

    SharePoint 2010

    Figure : Application Management section in SharePoint 2010
  3. Click on User Profile Service Application.
  4. Click on Manage Social Tags and Notes which is available in "My Site Settings" section.
    Social Tags in SharePoint
  5. Select Tags from the Type dropdown and enter the User.
  6. Click on Find.
  7. You could be able to see the Social Tags for the specified user.

Programmatically get all the social tags for the specified user:

Steps Involved:

  1. Open Visual Studio 2010 by going Start | All Programs | Microsoft Visual Studio 2010 | Right click on Microsoft Visual Studio 2010 and click on Run as administrator.
  2. Go to File tab, click on New and then click on Project.
  3. In the New Project dialog box, expand the Visual C# node, and then select the Windows node.
  4. In the Templates pane, select Console Application.
  5. Enter the Name and then click OK.
  6. In the solution explorer, right click on the solution and then click on Properties.
  7. Select the Application tab, check whether ".Net Framework 3.5" is selected for Target Framework.
  8. Select the Build tab, check whether "Any CPU" is selected for Platform Target.
  9. In the solution explorer, right click on the References folder and click on Add Reference.
  10. Add the following references.
    • Microsoft.SharePoint.dll
    • Microsoft.Office.Server.UserProfiles.dll
    • Microsoft.Office.Server.dll
  11. Double click on Program.cs and add the following Namespaces.
    • using Microsoft.Office.Server.SocialData;
    • using Microsoft.SharePoint;
    • using Microsoft.Office.Server;
    • using Microsoft.Office.Server.UserProfiles;
  12. Replace Program.cs with the following code snippet.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;  
    using Microsoft.Office.Server.SocialData;  
    using Microsoft.SharePoint;
    using Microsoft.Office.Server.UserProfiles;
    using Microsoft.Office.Server;

    namespace
    UserProfilesPOC
    {   
        public class Program
        {
            public static void Main(string[] args)
            {          
                using (SPSite site = new SPSite("https://servername.com/finance/Finance/"))
                {
                    //// Get the context of the service application
                    SPServiceContext context = SPServiceContext.GetContext(site);
     
                    ////UserProfileManager is used to create a UserProfile object and then retrieve the corresponding data from the user profile database.
                    UserProfileManager userProfileManager = new UserProfileManager(context);
     
                    ////Gets the UserProfile object for the specified account name
                    UserProfile userProfile = userProfileManager.GetUserProfil (@"domainname\username"); 
     
                    //// SocialTagManager - Contains methods and properties used to manipulate social tag data
                    SocialTagManager socialTagManager = new SocialTagManager(context);
     
                    //// GetTags(userProfile) method is used to retrieve an array of SocialTag objects for the specified user.
                    SocialTag[] tags = socialTagManager.GetTags(userProfile);
                    Console.WriteLine("Tags for the User Profile: " + userProfile.DisplayName);
                    foreach (SocialTag tag in tags)
                    {
                        Console.WriteLine("###############################################";
                        Console.WriteLine(" Tag Name : " + tag.Term.Name);
                        Console.WriteLine(" URL      : " + tag.Url.AbsoluteUri);
                        Console.WriteLine(" Title    : " + tag.Title;                                     
                    }
                    Console.ReadLine();              
                }
            }
        }
    }
     

  13. In the solution explorer, right click on the solution and click on Build.
  14. Hit F5.

Summary:

In this article you have seen how to get all the social tags for the specified user in SharePoint 2010 using object model.