Created
July 28, 2020 14:07
SF_13.1, SF_13.2, SF_13.3, SF_14.0, SF_14.1, SF_14.2, SF_14.3 - https://www.progress.com/documentation/sitefinity-cms/for-developers-create-comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Telerik.Sitefinity.Modules.News; | |
using Telerik.Sitefinity.News.Model; | |
using Telerik.Sitefinity.Services; | |
using Telerik.Sitefinity.Services.Comments; | |
using Telerik.Sitefinity.Services.Comments.Proxies; | |
using Telerik.Sitefinity.Web.UI; | |
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Comments | |
{ | |
public partial class CommentsSnippets | |
{ | |
public static IComment CreateNewsComment(Guid newsItemId, string content, IAuthor author, string ip) | |
{ | |
NewsManager manager = NewsManager.GetManager(); | |
// Get the news item | |
NewsItem newsItem = manager.GetNewsItem(newsItemId); | |
//Gets an instance of the comment service | |
var cs = SystemManager.GetCommentsService(); | |
var language = SystemManager.CurrentContext.Culture.Name; | |
var threadKey = ControlUtilities.GetLocalizedKey(newsItemId, language); | |
CommentsSnippets.EnsureNewsThreadExists(threadKey, author, newsItem.Title, manager, language, cs); | |
//new comment is created via the CommentProxy | |
var commentProxy = new CommentProxy(content, threadKey, author, ip); | |
//set the status of the commentProxy to published. By default it is WaitingForApproval | |
commentProxy.Status = StatusConstants.Published; | |
var comment = cs.CreateComment(commentProxy); | |
return comment; | |
} | |
/// <summary> | |
/// Ensures the news thread exists. | |
/// </summary> | |
/// <param name="threadKey">The thread key.</param> | |
/// <param name="author">The author.</param> | |
/// <param name="threadTitle">The thread title.</param> | |
/// <param name="manager">The manager.</param> | |
/// <param name="language">The language.</param> | |
/// <param name="cs">The cs.</param> | |
private static void EnsureNewsThreadExists(string threadKey, IAuthor author, string threadTitle, NewsManager manager, string language, ICommentService cs) | |
{ | |
ThreadFilter threadFilter = new ThreadFilter(); | |
threadFilter.ThreadKey.Add(threadKey); | |
var thread = cs.GetThreads(threadFilter).SingleOrDefault(); | |
if (thread == null) | |
{ | |
var groupKey = ControlUtilities.GetUniqueProviderKey(typeof(NewsManager).FullName, manager.Provider.Name); | |
CommentsSnippets.EnsureNewsGroupExists(groupKey, author, cs); | |
var culture = SystemManager.CurrentContext.AppSettings.GetCultureByName(language); | |
var threadProxy = new ThreadProxy(threadTitle, typeof(NewsItem).FullName, groupKey, author, culture) { Key = threadKey }; | |
thread = cs.CreateThread(threadProxy); | |
} | |
} | |
/// <summary> | |
/// Ensures the news group exists. | |
/// </summary> | |
/// <param name="groupKey">The group key.</param> | |
/// <param name="author">The author.</param> | |
/// <param name="cs">The cs.</param> | |
private static void EnsureNewsGroupExists(string groupKey, IAuthor author, ICommentService cs) | |
{ | |
GroupFilter groupFilter = new GroupFilter(); | |
groupFilter.GroupKey.Add(groupKey); | |
var group = cs.GetGroups(groupFilter).SingleOrDefault(); | |
if (group == null) | |
{ | |
var groupProxy = new GroupProxy("Group title", "news items in provider", author) { Key = groupKey }; | |
group = cs.CreateGroup(groupProxy); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment