Sitefinity CMS

Managing Read Status of Comments Send comments on this topic.
See Also
Developing with Sitefinity > Modules > Modules API > Generic Content > Comments > Managing Read Status of Comments

Glossary Item Box

There are several methods for managing the Read status of a comment:

Mark the specified comment of the given user as Read:

MarkCommentAsRead(IComment comment, string userName) Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get all content items
IList listOfContentItems = contentManager.GetContent();
if (listOfContentItems.Count > 0)
{
   
// get the first content item
   
Telerik.Cms.Engine.IContent secondContent = contentManager.GetContent(((Telerik.Cms.Engine.IContent)listOfContentItems[1]).ID);
   
// create a local variable to later be populated with the number of unread comments for the given user
   
int unreadCommentsNumber = 0;
   
// get all comments for the content item secondContent, sorted
   
IList listOfComments = contentManager.GetComments(secondContent.ID, false, "TextValue ASC", "admin", out unreadCommentsNumber);
   
if (listOfComments.Count > 0)
   {
       
foreach (Telerik.Cms.Engine.IComment commentItem in listOfComments)
           Response.Write(commentItem.Text +
" . And this is the number of unread comments for the admin user: " + unreadCommentsNumber.ToString() + "<br />");
   }
   Response.Write(
"<br />");
   
if (unreadCommentsNumber > 0)
   {
       
// get first comment
       
Telerik.Cms.Engine.IComment firstComment = contentManager.GetComment(((Telerik.Cms.Engine.IComment)listOfComments[0]).ID);
       
if (!firstComment.IsReadByUser("admin"))
       {
           
// mark read comment as Read
           
contentManager.MarkCommentAsRead(firstComment, "admin");
           
// get all comments and number of the Read ones
           
IList listOfComments2 = contentManager.GetComments(secondContent.ID, false, "TextValue ASC", "admin", out unreadCommentsNumber);
           
if (listOfComments2.Count > 0)
           {
               
foreach (Telerik.Cms.Engine.IComment commentItem in listOfComments2)
                   Response.Write(commentItem.Text +
" . And this is the number of unread comments for the admin user: " + unreadCommentsNumber.ToString() + "<br />");
           }
       }
       
else
           
Response.Write(" Already read by User");
   }
}

 

Mark the specified comments of the given user as Read:

MarkCommentsAsRead(IList<IComment> comments, string userName) Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get all content items
IList listOfContentItems = contentManager.GetContent();
if (listOfContentItems.Count > 0)
{
   
// get the first content item
   
Telerik.Cms.Engine.IContent secondContent = contentManager.GetContent(((Telerik.Cms.Engine.IContent)listOfContentItems[1]).ID);
   
// create a local variable to later be populated with the number of unread comments for the given user
   
int unreadCommentsNumber = 0;
   
// get all comments for the content item secondContent, sorted
   
IList listOfComments = contentManager.GetComments(secondContent.ID, false, "TextValue ASC", "admin", out unreadCommentsNumber);
   
if (listOfComments.Count > 0)
   {
       
// write number of comments & number of unread comments
       
foreach (Telerik.Cms.Engine.IComment commentItem in listOfComments)
           Response.Write(commentItem.Text +
" . And this is the number of unread comments for the admin user: " + unreadCommentsNumber.ToString() + "<br />");
   }
   Response.Write(
"<br />");
   
if (unreadCommentsNumber > 0)
   {
       
// create a list of IComment items
       
List<Telerik.Cms.Engine.IComment> commentList = new List<Telerik.Cms.Engine.IComment>();
       
// add each of the comment items from listOfComments to the new list
       
foreach (Telerik.Cms.Engine.IComment cmnt in listOfComments)
           commentList.Add(cmnt);
       
// mark all comment items in the list as read
       
contentManager.MarkCommentsAsRead(commentList, "admin");

       
// get all comments and number of Unread ones by the user
       
IList listOfComments2 = contentManager.GetComments(secondContent.ID, false, "TextValue ASC", "admin", out unreadCommentsNumber);
       
if (listOfComments2.Count > 0)
       {
           
// write number of comments & number of unread comments after usage of the MarkCommentsAsRead method
           
foreach (Telerik.Cms.Engine.IComment commentItem in listOfComments2)
               Response.Write(commentItem.Text +
" . And this is the number of unread comments for the admin user: " + unreadCommentsNumber.ToString() + "<br />");
       }
   }
}  

 

Mark the specified comment of the given user as Unread:

MarkCommentAsUnread(IComment comment, string userName) Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get all content items
IList listOfContentItems = contentManager.GetContent();
if (listOfContentItems.Count > 0)
{
   
// get the first content item
   
Telerik.Cms.Engine.IContent secondContent = contentManager.GetContent(((Telerik.Cms.Engine.IContent)listOfContentItems[1]).ID);
   
// create a local variable to later be populated with the number of unread comments for the given user
   
int unreadCommentsNumber = 0;
   
// get all comments for the content item secondContent, sorted
   
IList listOfComments = contentManager.GetComments(secondContent.ID, false, "TextValue ASC", "admin", out unreadCommentsNumber);
   
if (listOfComments.Count > 0)
   {
       
foreach (Telerik.Cms.Engine.IComment commentItem in listOfComments)
           Response.Write(commentItem.Text +
" . And this is the number of unread comments for the admin user: " + unreadCommentsNumber.ToString() + "<br />");
   }
   Response.Write(
"<br />");
   
if (unreadCommentsNumber > 0)
   {
       
// get first comment
       
Telerik.Cms.Engine.IComment firstComment = contentManager.GetComment(((Telerik.Cms.Engine.IComment)listOfComments[0]).ID);
      
       
if (firstComment.IsReadByUser("admin"))
       {
           
// mark read comment as Unread
           
contentManager.MarkCommentAsUnread(firstComment, "admin");
           
// get all comments and number of Unread ones by the user
           
IList listOfComments2 = contentManager.GetComments(secondContent.ID, false, "TextValue ASC", "admin", out unreadCommentsNumber);
           
if (listOfComments2.Count > 0)
           {
               
foreach (Telerik.Cms.Engine.IComment commentItem in listOfComments2)
                   Response.Write(commentItem.Text +
" . And this is the number of unread comments for the admin user: " + unreadCommentsNumber.ToString() + "<br />");
           }
       }
       
else
           
Response.Write(" Already read by User");
   }
}  

 

Mark the specified comments of the given user as Unread:

MarkCommentsAsUnread(IList<IComment> comments, string userName) Copy Code
// create new instance of ContentManager
Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
// get all content items
IList listOfContentItems = contentManager.GetContent();
if (listOfContentItems.Count > 0)
{
   
// get the first content item
   
Telerik.Cms.Engine.IContent secondContent = contentManager.GetContent(((Telerik.Cms.Engine.IContent)listOfContentItems[1]).ID);
   
// create a local variable to later be populated with the number of unread comments for the given user
   
int unreadCommentsNumber = 0;
   
// get all comments for the content item secondContent, sorted
   
IList listOfComments = contentManager.GetComments(secondContent.ID, false, "TextValue ASC", "admin", out unreadCommentsNumber);
   
if (listOfComments.Count > 0)
   {
       
// write number of comments & number of unread comments
       
foreach (Telerik.Cms.Engine.IComment commentItem in listOfComments)
           Response.Write(commentItem.Text +
" . And this is the number of unread comments for the admin user: " + unreadCommentsNumber.ToString() + "<br />");
   }
   Response.Write(
"<br />");
   
// create a list of IComment items
   
List<Telerik.Cms.Engine.IComment> commentList = new List<Telerik.Cms.Engine.IComment>();
   
// add each of the comment items from listOfComments to the new list
   
foreach (Telerik.Cms.Engine.IComment cmnt in listOfComments)
       commentList.Add(cmnt);
   
// mark all comment items in the list as unread
   
contentManager.MarkCommentsAsUnread(commentList, "admin");

   
// get all comments and number of Unread ones by the user
   
IList listOfComments2 = contentManager.GetComments(secondContent.ID, false, "TextValue ASC", "admin", out unreadCommentsNumber);
   
if (listOfComments2.Count > 0)
   {
       
// write number of comments & number of unread comments after usage of the MarkCommentsAsRead method
       
foreach (Telerik.Cms.Engine.IComment commentItem in listOfComments2)
           Response.Write(commentItem.Text +
" . And this is the number of unread comments for the admin user: " + unreadCommentsNumber.ToString() + "<br />");
   }
}


 

See Also