locked
Social comments RRS feed

  • Question

  • How do i retrieve all the comments by all users for an url? I can only get the comments for the logged in user. That does not seem very social to me.
    I have this so far

    SocialCommentManager socialCommentManager = GetSocialCommentManager();
                           SPServiceContext context = SPServiceContext.GetContext(SPContext.Current.Web.Site);

                UserProfileManager profileManager = new UserProfileManager(context);
                  List<SocialComment> everyonesComments =new List<SocialComment>() ;
               
                foreach (UserProfile profile in profileManager)
                {

                    SocialComment[] allComments = socialCommentManager.GetComments(profile, this.Request.Url);
                    foreach (SocialComment sc in allComments)
                    {
                        everyonesComments.Add(sc);
                    }
                }
             
                return everyonesComments;

    Thursday, March 24, 2011 9:44 PM

All replies

  • This will do it.

     public static void GetCommentsForUrl()
     {
          int count = 0;
    
          SPServiceContext context = SPServiceContext.GetContext(SPServiceApplicationProxyGroup.Default,
            SPSiteSubscriptionIdentifier.Default);
    
          SocialCommentManager scm = new SocialCommentManager(context);
    
          SocialComment[] comments = scm.GetComments(new Uri("http://basesmc2008/Shared%20Documents/Forms/AllItems.aspx"));
    
          
          if (comments != null)
          {
            var results = comments.Where(t => t.Title == "Cool");
            if (results != null)
              count = results.Count();
    
          }
          
    }
    

    Blog | SharePoint Field Notes Dev Tool | ClassMaster
    Friday, March 25, 2011 1:30 AM
  • Thanks for the help, but it doesn't work.

    I was looking to retrieve a list of comments on a url by everyone.. I can get the correct count of comments, but still cant get it to return all the text.

    Friday, March 25, 2011 4:37 PM
  • I dont understand what you mean by all the text.
    Blog | SharePoint Field Notes Dev Tool | ClassMaster
    Friday, March 25, 2011 5:16 PM
  • OK - this is what I have.. could you explain whats wrong with it?

    Thanks again! :)

    public SocialComment[] GetComments()
    {
         SPServiceContext context = SPServiceContext.GetContext(SPContext.Current.Web.Site);

                SocialRatingManager socialRatingManager = new SocialRatingManager(context);         

            comments = socialCommentManager.GetComments(this.Request.Url);
          return comments;
     

    }

    Friday, March 25, 2011 5:49 PM
  • You are using a SocialRatingManager and socialCommentManager is not declared, so the code wont compile. You should be using the SocialCommentManager as posted in my code.
    Blog | SharePoint Field Notes Dev Tool | ClassMaster
    Friday, March 25, 2011 6:32 PM
  • I fixed that.

    So it returns my comments, but not comments other users have made on my page.

    Is it a privacy /security issue?

    Friday, March 25, 2011 6:43 PM
  • The user who is executing the code must be added as an administrator to the User Profile Service Application and be given the "Manage Social Data" permission. This is under central administration -- Manage Service Applications.
    Blog | SharePoint Field Notes Dev Tool | ClassMaster
    Friday, March 25, 2011 6:46 PM
  • So everyone has to be an admin to run this code? I checked and all authenticated users have the manage social data permissions.

     thank you.

    Friday, March 25, 2011 7:25 PM
  •  Yes in order to see other comments the user should be either the site collection administrator or part of the administrators of the user profile service application as described previously. And yes it is a privacy issue.
    Blog | SharePoint Field Notes Dev Tool | ClassMaster
    Saturday, March 26, 2011 1:10 AM
  • Seems weird to me its locked down like that..

    Anyhow I figured out a hacky way around it if anyone else is interested in displaying all user comments.

     public List<SocialComment> GetComments()
            {
                List<SocialComment> allComments = new List<SocialComment>();
                SPServiceContext context = SPServiceContext.GetContext(SPContext.Current.Web.Site);
                UserProfileManager profileManager = new UserProfileManager(context);

                foreach (UserProfile profile in profileManager)
                {
                    using (SPSite site = new SPSite(this.Request.Url.AbsoluteUri))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;
                            SPUser user = web.EnsureUser(profile.DisplayName);
                            WindowsIdentity identity = WindowsIdentity.GetCurrent();
                            typeof(WindowsIdentity).GetField("m_name", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(identity, user.LoginName);
                            HttpContext.Current.User = new GenericPrincipal(identity, new string[0]);
                            SocialCommentManager socialCommentManager = new SocialCommentManager(SPServiceContext.GetContext(HttpContext.Current));
                            SocialComment[] comments = socialCommentManager.GetComments(this.Request.Url);
                            foreach (SocialComment sc in comments)
                            {
                                allComments.Add(sc);
                            }
                            web.AllowUnsafeUpdates = false;
                        }
                    }
                }

                allComments.Sort(CompareCommentDate);
                return allComments;
            }

            public static int CompareCommentDate(SocialComment x, SocialComment y)
            {
                return x.LastModifiedTime.CompareTo(y.LastModifiedTime);
            }

    Monday, March 28, 2011 4:50 PM
  • hmmm, that code looks familiar.
    Blog | SharePoint Field Notes Dev Tool | ClassMaster
    Monday, March 28, 2011 6:25 PM
  • Below Method will retrieve all comments for the Specified URL using WebService
    public void getComments(string GetRatingForThisURL)
            {
                try
                {
                  

                        SocialCommentsData.SocialDataService mySocialDataService = new SocialCommentsData.SocialDataService();
                        mySocialDataService.Credentials = System.Net.CredentialCache.DefaultCredentials;
                        mySocialDataService.Url = SPContext.Current.Site.Url+"/_vti_bin/socialdataservice.asmx";
                        SocialCommentsData.SocialCommentDetail[] comments = mySocialDataService.GetCommentsOnUrl(GetRatingForThisURL,null,null,null);

                                dt = new DataTable();
                                dt.Columns.Add("Comments", typeof(String));
                                dt.Columns.Add("Postedby", typeof(String));
                                dt.Columns.Add("postedon", typeof(String));
                                foreach (SocialCommentsData.SocialCommentDetail comment in comments)
                                {
                                    DataRow dr = dt.NewRow();
                                    dr["Comments"] = comment.Comment;
                                    dr["Postedby"] = comment.Owner;
                                    dr["postedon"] = comment.LastModifiedTime;
                                    dt.Rows.Add(dr);
                                }
                              
                           

                }
                catch
                {
                }
            }
    Aravinthan B
    Monday, May 2, 2011 7:30 AM
  • AFAIK this

    http://technet.microsoft.com/en-us/library/ff608006.aspx

    and this

    http://blogs.msdn.com/b/sanjaynarang/archive/2011/06/12/custom-note-board-web-part-socialcommentmanager-social-security-trimming-and-search.aspx

    
    
    

    If the search service is not set up, it is considered that the user does not have permission...

    
    
    

    Marat Bakirov


    Thursday, June 21, 2012 12:43 PM