Programmatically get all the Social Comments in SharePoint 2010


In this article you will be seeing how to get all the Social Comments in SharePoint 2010 using the object model. Social Comments can be created for any specified URL. SocialCommentManager object is used to create Social Comment for any specified URL and by using the GetComments methods you can get all the comments.

Here you will be seeing how to get all the social comments in SharePoint 2010 using the SharePoint Object Model.

View all the social comments 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.

spoint1.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.

5.  By selecting Notes from the Type dropdown and by filling the values you could be able to see all the social comments based on your needs.

Program

matically get all the social comments:

Social Comments can be retrieved by using the following methods.

1.  GetComments(Uri) – Used to get  all of the social comments created for the specified URL.

2.  GetComments(UserProfile) - Used to get all of the social comments created by the specified user.

3.  GetComments(Uri, Int32) - Used to get all of the social comments created for the specified URL, up to the specified maximum number.

4.  GetComments(UserProfile, Int32) - Used to get social comments created by the specified user, up to the specified maximum number to retrieve.

5.  GetComments(UserProfile, Uri) - Used to get all of the social comments created by the specified user for the specified URL.

6.  GetComments(Uri, Int32, Int32) - Used to get all of the social comments created for the specified URL, up to the specified maximum number and starting at the specified zero-based index.

7.  GetComments(UserProfile, Int32, Int32) - Used to get social comments created by the specified user, up to the specified maximum number to retrieve, and starting at the specified zero-based index.

You will be seeing how to get the social comments by using all the methods mentioned above.

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

3.  Microsoft.Office.Server.dll

4.  System.Web

11. Double click on Program.cs and add the following Namespaces.

1.  using Microsoft.Office.Server.SocialData;

2.  using Microsoft.SharePoint; 

3.  using Microsoft.Office.Server;

4.  using Microsoft.Office.Server.UserProfiles;

12. Replace Program.cs with the following code snippet.

GetComments(Uri) method:

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
       using Microsoft.SharePoint;
     using Microsoft.Office.Server;
     using Microsoft.Office.Server.SocialData;
     using Microsoft.Office.Server.UserProfiles;
 
     namespace UserProfileCodes
     {
            class Program
             {
                      static void Main(string[] args)
              {
                  Uri uri = new Uri("https://servername.com/hr/HR/Shared%20Documents/Forms/AllItems.aspx");
                          using (SPSite site = new SPSite("https://servername.com/hr/HR/"))
                          {
                                 SPServiceContext context = SPServiceContext.GetContext(site);               
                                     SocialCommentManager socialCommentManager = new SocialCommentManager(context);               
                                  SocialComment[] comments = socialCommentManager.GetComments(uri);
                                   Console.WriteLine("Comments for the URL: "+uri.AbsoluteUri);
                                     foreach (SocialComment comment in comments)
                                    {
                                    Console.WriteLine("####################################");
                                    Console.WriteLine("Comment      : "+ comment.Comment);
                                      Console.WriteLine("Comment Title: " + comment.Title);
                                     Console.WriteLine("Comment owner: " + comment.Owner.DisplayName);
                                   }
                                   Console.ReadLine(); 
                  }

                 }

                     }

      }  

GetComments(UserProfile) method:

            using (SPSite site = new SPSite("https://servername.com/hr/HR/"))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                UserProfileManager userProfileManager = new UserProfileManager(context);
                UserProfile userProfile = userProfileManager.GetUserProfile(@"domainName\userName");
                SocialCommentManager socialCommentManager = new SocialCommentManager(context);
                SocialComment[] comments = socialCommentManager.GetComments(userProfile);
                Console.WriteLine("Comments for the user: "+userProfile.DisplayName);

                foreach (SocialComment comment in comments)
                {
                    Console.WriteLine("####################################");
                    Console.WriteLine("Comment      : "+  comment.Comment);
                    Console.WriteLine("Comment Title: " + comment.Title);
                    Console.WriteLine("Comment URL  : " + comment.Url.AbsoluteUri);
                }
                Console.ReadLine();
            } 

GetComments(Uri, Int32) method:

            Uri uri = new Uri("https://serverName.com/hr/HR/Shared%20Documents/Forms/AllItems.aspx");
            using (SPSite site = new SPSite("https://serverName.com/hr/HR/"))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                UserProfileManager userProfileManager = new UserProfileManager(context);
                SocialCommentManager socialCommentManager = new SocialCommentManager(context);
                int maxItems = 2;
                SocialComment[] comments = socialCommentManager.GetComments(uri, maxItems);
                Console.WriteLine("Comments for the URL: "+uri.AbsoluteUri);

                foreach (SocialComment comment in comments)
                {
                    Console.WriteLine("####################################");
                    Console.WriteLine("Comment      : "+  comment.Comment);
                    Console.WriteLine("Comment Title: " + comment.Title);
                    Console.WriteLine("Comment User : " + comment.Owner.DisplayName);
                }
                Console.ReadLine();
            }

GetComments(UserProfile, Int32) method:

            using (SPSite site = new SPSite("https://serverName.com/hr/HR/"))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                UserProfileManager userProfileManager = new                                          UserProfileManager(context);
                UserProfile userProfile = userProfileManager.GetUserProfile(@"domainName\userName");
                SocialCommentManager socialCommentManager = new SocialCommentManager(context);
                int maxItems = 2;
                SocialComment[] comments = socialCommentManager.GetComments(userProfile, maxItems);
                Console.WriteLine("Comments for the user: "+userProfile.DisplayName);

                foreach (SocialComment comment in comments)
                {
                    Console.WriteLine("####################################");
                    Console.WriteLine("Comment      : "+  comment.Comment);
                    Console.WriteLine("Comment Title: " + comment.Title);
                    Console.WriteLine("Comment URL  : " + comment.Url.AbsoluteUri);
                }
                Console.ReadLine();
            }

GetComments(UserProfile, Uri) method:

            Uri uri = new Uri("https://serverName.com/hr/HR/Shared%20Documents/Forms/AllItems.aspx");
            using (SPSite site = new SPSite("https://serverName.com/hr/HR/"))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                UserProfileManager userProfileManager = new UserProfileManager(context);
                UserProfile userProfile = userProfileManager.GetUserProfile(@"domainName\serverName");
                SocialCommentManager socialCommentManager = new SocialCommentManager(context);               
                SocialComment[] comments = socialCommentManager.GetComments(userProfile,uri);
                Console.WriteLine("Comments for the user: "+userProfile.DisplayName);

                foreach (SocialComment comment in comments)
                {
                    Console.WriteLine("####################################");
                    Console.WriteLine("Comment      : "+  comment.Comment);
                    Console.WriteLine("Comment Title: " + comment.Title);
                    Console.WriteLine("Comment URL  : " + comment.Url.AbsoluteUri);
                }
                Console.ReadLine();
            }

GetComments(Uri, Int32, Int32) method:

            Uri uri = new Uri("https://serverName.com/hr/HR/Shared%20Documents/Forms/AllItems.aspx");
            using (SPSite site = new SPSite("https://serverName.com/hr/HR/"))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);              
                SocialCommentManager socialCommentManager = new SocialCommentManager(context);
                int maxItems = 2;
                int index = 2;
                SocialComment[] comments = socialCommentManager.GetComments(uri,maxItems,index);
                Console.WriteLine("Comments for the URL: "+uri.AbsoluteUri);

                foreach (SocialComment comment in comments)
                {
                    Console.WriteLine("####################################");
                    Console.WriteLine("Comment      : "+  comment.Comment);
                    Console.WriteLine("Comment Title: " + comment.Title);
                    Console.WriteLine("Comment URL  : " + comment.Owner.DisplayName);
                }
                Console.ReadLine();
            }

GetComments(UserProfile, Int32, Int32) method:

            using (SPSite site = new SPSite("https://serverName.com/hr/HR/"))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                UserProfileManager userProfileManager = new UserProfileManager(context);
                UserProfile userProfile = userProfileManager.GetUserProfile(@"domainName\userName");
                SocialCommentManager socialCommentManager = new SocialCommentManager(context);
                int maxItems = 2;
                int index = 2;
                SocialComment[] comments = socialCommentManager.GetComments(userProfile,maxItems,index);
                Console.WriteLine("Comments for the user: "+userProfile.DisplayName);

                foreach (SocialComment comment in comments)
                {
                    Console.WriteLine("####################################");
                    Console.WriteLine("Comment      : "+  comment.Comment);
                    Console.WriteLine("Comment Title: " + comment.Title);
                    Console.WriteLine("Comment URL  : " + comment.Url.AbsoluteUri);|
                }                
                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 comments in SharePoint 2010 using object model.

Up Next
    Ebook Download
    View all
    Learn
    View all