For developers: Create comments (6.1 and below)
To create a news comment you can use the native API or the fluent API.
To create a news comment using the native API, you must use the NewsManager
class. For more information, see For developers: Create comments.
NOTE: When creating the comment, you must assign it to the live version of the news item. For more information about news, see For developers: News.
Creating a news comment using native API
When creating a news comment through the native API, you must perform the following:
-
Get the parent news item.
First, you must get the parent news item for the comment.
For more information about finding specific news items, see For developers: Query news items.
-
Get the live version of the news item.
You must get the live version of the news item to assign the comment to it.
-
Create new comment.
After you get the parent news item, you must create new comment for it.
-
Set the required properties.
When creating a new comment, it is recommended to set at least the following properties:
-
Content
-
AuthorName
-
DateCreated
You can also set any other properties (e.g. Email, Website, etc.) in this step.
-
Save the comment.
Save all changes that you have made to the comment.
The following code creates a news comment with the specified ID, Content and AuthorName through the native API.
public
void
CreateNewsCommentNativeAPI(Guid commentId, Guid masterNewsItemId,
string
content,
string
authorName)
{
NewsManager manager = NewsManager.GetManager();
// Get the news item
NewsItem newsItem = manager.GetNewsItems().Where(nI => nI.Id == masterNewsItemId).FirstOrDefault();
if
(newsItem !=
null
)
{
// Get the live version of the news item
newsItem = manager.Lifecycle.GetLive(newsItem)
as
NewsItem;
// Create new comment
Comment comment = manager.CreateComment(newsItem, commentId);
// Set the comment properties
comment.Content = content;
comment.AuthorName = authorName;
comment.DateCreated = DateTime.Now;
// Save the changes
manager.SaveChanges();
}
}
First, you initialize the NewsManager
. You get the parent news item using the GetNewsItems
method. Then, you get the live version of the news item using Lifecycle.GetLive
.
For more information about finding specific news items, see For developers: Query news items.
You create the comment using the CreateComment
method of the NewsManager
class and passing the Id
of the news item. Note that you can create a comment with either predefined or auto-generated ID
depending on which overload of the method you use. Then, you set the properties of the comment. It is recommended to set at least the following properties: Content, AuthorName and DateCreated. Finally, you save the changes.
Creating a news comment using fluent API
When creating a news comment through the fluent API, you must perform the following:
-
Check whether the parent news item exists.
First, you must check whether the specified parent news item exists.
-
Get the parent news item.
If it exists, you must get the parent news item.
For more information about finding specific news items, see For developers: Query news items.
-
Get the live version of the news item.
You must get the live version of the news item to assign the comment to it.
-
Create new comment.
After you get the parent news item, you must create new comment for it.
-
Set the required properties.
When creating a new comment, it is recommended to set at least the following properties:
-
Content
-
AuthorName
-
DateCreated
You can also set any other properties (for example, Email, Website, etc.) in this step.
-
Save the comment.
Save all changes that you have made to the comment.
The following code creates a news comment with the specified ID, Content and AuthorName through the fluent API.
public
Guid CreateNewsCommentFluentAPI(Guid masterNewsItemId,
string
content,
string
authorName)
{
int
count = 0;
Guid commentId = Guid.Empty;
// Check whether the parent news item exists
App.WorkWith().NewsItems().Where(nI => nI.Id == masterNewsItemId).Count(
out
count);
if
(count > 0)
{
App.WorkWith().NewsItem(masterNewsItemId).GetLive().Comment().CreateNew().Do(c =>
{
// Get the Id of the comment
commentId = c.Id;
// Set the comment properties
c.Content = content;
c.AuthorName = authorName;
c.DateCreated = DateTime.Now;
}).SaveChanges();
}
return
commentId;
}
First, you check whether the specified parent news item exists using the plural facade of the news.
For more information about finding specific news items, see For developers: Query news items.
If it exists, you get the news item using the singular facade. Then, you get the live version of the facade using the GetLive method of the facade. You get the singular facade of the comment. Then, you create the new comment using the CreateNew method of the facade. Then, you set the properties of the comment by calling the Do method of the facade. It is recommended to set at least the following properties:Content, AuthorName and DateCreated. Then, you save the changes. Finally, you return the Id
of the comment.