For developers: Query comments (6.1 and below)
Sitefinity CMS allows you to query for specific comments.
To query for specific news comments you can use the Native API or the Fluent API.
Querying a single news comment
When querying for specific news comment by its ID, you must perform the following:
-
Get the news item.
First, get an instance of the master version of the news item that corresponds to the specified ID.
For more information about finding specific news items, see For developers: Query news items.
-
Get the live version.
The instance that corresponds to the ID argument is the master version of the news item. You can get the live version explicitly.
-
Get the comment.
Get the comment that corresponds to the specified ID.
-
Return the comment.
Return the comment. If an item with the specified ID does not exist, or if it has no live version, you return null.
The following examples query for the news comment with the specified ID.
Native API
public
Comment GetCommentByIdNativeAPI(Guid masterNewsId, Guid commentId)
{
NewsManager newsManager = NewsManager.GetManager();
Comment comments =
null
;
// Check whether the news item exists
NewsItem news = newsManager.GetNewsItems().Where(nI => nI.Id == masterNewsId).FirstOrDefault();
if
(news !=
null
)
{
// Get the live version
news = newsManager.Lifecycle.GetLive(news)
as
NewsItem;
// Get the comment
comments = newsManager.GetComments().Where(c => c.CommentedItemID == news.Id && c.Id == commentId).SingleOrDefault();
}
return
comments;
}
First, you get an instance of the NewsManager class. You get the specified news item by querying all items and filtering the collection by the ID property.
For more information about finding specific news items, see For developers: Query news items.
If the item exists, you get its live version. Then, you get the comment by calling GetComments and filtering based on theCommentedItemID and Id properties. Finally, you return the comment.
Fluent API
public
Comment GetCommentByIdFluentAPI(Guid masterNewsId, Guid commentId)
{
int
newsCount = 0;
Comment comment =
null
;
// Check whether the news item exists
App.WorkWith().NewsItems().Where(nI => nI.Id == masterNewsId).Count(
out
newsCount);
if
(newsCount > 0)
{
// Get the live version
var liveNewsItem = App.WorkWith().NewsItem(masterNewsId).GetLive();
if
(liveNewsItem !=
null
)
{
int
commentCount = 0;
// Check whether the comment exists
liveNewsItem.Comments().Where(c => c.Id == commentId).Count(
out
commentCount);
if
(commentCount > 0)
{
// Get the comment
comment = liveNewsItem.Comment(commentId).Get();
}
}
}
return
comment;
}
First, you use the plural facade of the news item to assure that the item with the specified Id exists.
For more information about finding specific news items, see For developers: Query news items.
Then, you use the GetLive method of the singular facade to get the instance of the live version. You initialize the plural facade of the comments by calling Comments. Then, you get the comment by filtering based on the Id property. Finally, you return the comment.
Querying all news comments
When querying all news comments by the Id of the master version of the news item, you must perform the following:
-
Get the news item.
First, get an instance of the master version of the news item that corresponds to the specified ID.
For more information about finding specific news items, see For developers: Query news items.
-
Get the live version.
The instance that corresponds to the ID argument is the master version of the news item. You can get the live version explicitly.
-
Get the comments.
Get all comments that correspond to the specified news item.
-
Return the comment.
Finally, return the comments in a list.
The following examples query all news comments by the Id of the master version of the news item.
Native API
public
List<Comment> GetCommentsByNewsIdNativeAPI(Guid masterNewsId)
{
NewsManager newsManager = NewsManager.GetManager();
List<Comment> comments =
null
;
// Check whether the news item exists
NewsItem news = newsManager.GetNewsItems().Where(nI => nI.Id == masterNewsId).FirstOrDefault();
if
(news !=
null
)
{
news = newsManager.Lifecycle.GetLive(news)
as
NewsItem;
// Get the comments
comments = newsManager.GetComments().Where(c => c.CommentedItemID == news.Id).ToList();
}
return
comments;
}
First, you get an instance of the NewsManager class. You get the specified news item by querying all items and filtering the collection by the ID property.
For more information about finding specific news items, see For developers: Query news items.
If the item exists, you get its live version. Then, you get the comments by calling GetComments and filtering based on theCommentedItemID property. Finally, you return the comments in a list.
Fluent API
public
List<Comment> GetCommentsByNewsIdFluentAPI(Guid masterNewsId)
{
int
newsCount = 0;
List<Comment> comments =
null
;
// Check whether the news item exists
App.WorkWith().NewsItems().Where(nI => nI.Id == masterNewsId).Count(
out
newsCount);
if
(newsCount > 0)
{
// Check whether the news item exists
var liveNewsItem = App.WorkWith().NewsItem(masterNewsId).GetLive();
if
(liveNewsItem !=
null
)
{
// Get the comments
comments = liveNewsItem.Comments().Get().ToList();
}
}
return
comments;
}
First, you use the plural facade of the news item to assure that the item with the specified Id exists.
For more information about finding specific news items, see For developers: Query news items.
Then, you use the GetLive method of the singular facade to get the instance of the live version. You initialize the plural facade of the comments by calling Comments. Then, you get the comment by calling the Get method of the facade. Finally, you return the comments in a list.