Programmatically get all the Social Tags for the specified URL in SharePoint 2010


In this article you will be seeing how to get all the Social Tags for the specified URL in SharePoint 2010 using the object model.

A 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 URL 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.

    Share1.gif

    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.

    Share2.gif
     
  5. Select Tags from the Type dropdown and enter the URL.
  6. Click on Find.
  7. You could be able to see the Social Tags for the specified URL.


Programmatically get all the social tags for the specified URL:

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.
    1. Microsoft.SharePoint.dll
    2. Microsoft.Office.Server.UserProfiles.dll
  11. Double click on Program.cs and add the following Namespaces.
    1. using Microsoft.Office.Server.SocialData;
    2. using Microsoft.SharePoint;
     
  12. Replace Program.cs with the following code snippet.

    namespace GetallSocialTags
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;  
        using Microsoft.Office.Server.SocialData;  
        using Microsoft.SharePoint;   

        public class Program
        {
            public static void Main(string[] args)
            {

                //// Uri - required to get all the social tags for the specified uri
                Uri uri = new Uri("https://servername.com/finance/Finance/");
                using (SPSite site = new SPSite("https://servername.com/finance/Finance/"))
                {
                    //// Get the context of the service application
                    SPServiceContext context = SPServiceContext.GetContext(site);

                    //// SocialTagManager - Contains methods and properties used to manipulate social tag data
                    SocialTagManager socialTagManager = new SocialTagManager(context);

                    //// GetTags(Uri) method is used to retrieve an array of SocialTag objects that are owned by the current user and that contain the specified URL.
                    SocialTag[] tags = socialTagManager.GetTags(uri);
                    Console.WriteLine("Tags for the URL: " + uri.AbsoluteUri);
                    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.WriteLine(" Owner    : " + tag.Owner.DisplayName);                   
                    }
                    Console.ReadLine();              
                }
            }
        }
    }


     

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

  14. Hit F5.

Summary:

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