Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

In my custom webpart, I want to show the number of comments. I try to get this throug SocialCommentManager.GetComments and then return comments.Count.

The problem is: I get only comments for the current user. Say, there are three comments on a page, one is mine. On startpage in a custom webpart I see that there is only one comment.

Even if I run with elevated, the users get only the number of their comments.

int number;
SocialComment[] comments = null;
SPSecurity.RunWithElevatedPrivileges(delegate
     {
         using (SPSite esite = new SPSite(SPContext.Current.Site.ID))
         {
             var sc = SPServiceContext.GetContext(esite);
             var soc = new SocialCommentManager(sc);
             comments = soc.GetComments(uri, maxNumberOfComments);
             number = comments.Count();
         }
});

While logged in as System Account, I get all (System Account is set as the administrator on User Profile Service).

Any ideas?

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

I found a solution: You must renew the HttpContext.

int number;
SocialComment[] comments = null;    
SPSecurity.RunWithElevatedPrivileges(delegate
         {
             using (SPSite esite = new SPSite(SPContext.Current.Site.ID))
             {
                 var ctx = HttpContext.Current;
                 try
                 {
                     Log.Info("Trying to get comments");                                                             
                     var sc = SPServiceContext.GetContext(esite);
                     HttpContext.Current = null;
                     var soc = new SocialCommentManager(sc);
                     comments = soc.GetComments(uri, maxNumberOfComments);
                     number = comments.Count();
                 }
                 catch (Exception ex)
                 {
                     Log.Error(ex);
                 }
                 finally
                 {
                     HttpContext.Current = ctx;
                 }            
             }
    });
share|improve this answer
    
I am accepting my own solution. If someone finds out how to solve it a better way, I'll accept the new solution with a great pleasure. –  Anatoly Mironov Oct 3 '11 at 11:11
add comment

You can use the override with the Uri.

Check here please. http://msdn.microsoft.com/en-us/library/microsoft.office.server.socialdata.socialcommentmanager.getcomments.aspx

share|improve this answer
    
if you see the documentation the getcomments has different override methods, meaning different parameters can be passed, check the one that only passes the Uri without user and you will get all the comments for that uri regardless of the user. –  L.V. Sharepoint Architect Sep 29 '11 at 8:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.