Number of comments with SocialCommentManager.GetComments
-
29-11-2019 - |
質問
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?
解決
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;
}
}
});
他のヒント
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