Creating comments

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 Creating a news comment using Native API.

To create a news comment using the Fluent API, you must use the news facade. For more information, see Creating a news comment using Fluent API.

NOTE: When creating the comment, you must assign it to the live version of the news item. For more information about news, see News.

Creating a news comment using Native API

When creating a news comment through the Native API, you must perform the following:

  1. 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 Querying news items.

  2. Get the live version of the news item.

    You must get the live version of the news item to assign the comment to it.

  3. Create new comment.

    After you get the parent news item, you must create new comment for it.

  4. 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. EmailWebsite, etc.) in this step.

  5. Save the comment.

    Save all changes that you have made to the comment.

The following code creates a news comment with the specified IDContent 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 Querying 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: ContentAuthorName 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:

  1. Check whether the parent news item exists.

    First, you must check whether the specified parent news item exists.

  2. Get the parent news item.

    If it exists, you must get the parent news item.

    For more information about finding specific news items, see Querying news items.

  3. Get the live version of the news item.

    You must get the live version of the news item to assign the comment to it.

  4. Create new comment.

    After you get the parent news item, you must create new comment for it.

  5. 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. EmailWebsite, etc.) in this step.

  6. Save the comment.

    Save all changes that you have made to the comment.

The following code creates a news comment with the specified IDContent 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 Querying 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:ContentAuthorName and DateCreated. Then, you save the changes. Finally, you return the Id of the comment.

See Also

Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK