Mono Support Programmatically add post to Discussion Board (Closed)

Viewed 25410 time(s), 4 post(s), 10/4/2011 3:44:35 PM - by Maxim
10/4/2011 3:53:14 PM
319 Reputation 30 Total posts

Hi! How to programmatically add post to Discussion Board, and some comments to post...

1
10/5/2011 6:39:53 AM
15993 Reputation 2214 Total posts

Hi,

I suppose you meant " How to programmatically add topic to Discussion Board, and some comments to topic..." ? Below is the sample code that you can modify and create a Discussion topic and then discussion message entity.

Create a DiscussionTopicEntity

SnDiscussionTopicEntity entity = DiscussionRepository.GetInstance().EnsureTopicExists(BoardId, TopicId, UserId, title, maxTags)

Few notes:
1. You need to get the BoardId where you want to create a Topic
2. Pass in the id of the Topic as an Guid to get the proper PK
3. UserId parameter specifies the owner / creator of the Topic - you can use SecurityUtility.GetUserId() if you want to pass in the current user id

Create a DiscussionMessageEntity (Post)
SnDiscussionMessageEntity entity = DiscussionRepository.GetInstance().PostMessage(MessageId, TopicId, UserId, message);

Few notes:
1. You need to get the TopicId of the previously created topic you can do that by using the topic entity property Id
2. Pass in the id of the Message as an Guid to get the proper PK
3. UserId parameter specifies the owner / creator of the Message - you can use SecurityUtility.GetUserId() if you want to pass in the current user id

If you want to implement AntiXSS or SPAM security while posting a message you can use the following code

To implement AntiXSS security on the message html content use
HtmlFormatter.SanitizeHtml(messageContent, false)
To implement AntiXSS security on the message plain text content use
HtmlFormatter.SanitizeHtml(messageContent, true)

To implement SPAM check you can use the following
using (DiscussionRepository discussionRepository = DiscussionRepository.GetInstance())
{
    ...
    if (Message != null)
    {
        if (!IsDiscussionEditableByUser(BoardId) && this.ApprovalMode != CommentApprovalMode.None)
        {
            if (this.ApprovalMode == CommentApprovalMode.Manual)
                Message.IsApproved = false;
            else
                Message.IsApproved = true;
            MonoXSpamContentValidator validator = new MonoXSpamContentValidator();
            try
            {
                validator.ValidateComment(Message);                       
            }
            catch
            {
                Message.IsSpam = false;
                Message.IsApproved = true;                           
            }                       
        }
        else
        {
            Message.IsSpam = false;
            Message.IsApproved = true;
        }
        discussionRepository.SaveEntity(Message, true);
    }
    ...
}

I hope I have covered everything you need, let us know if you need anything else.

Regards

2
10/5/2011 6:58:55 AM
319 Reputation 30 Total posts

Thank you,all works!

3
10/5/2011 7:09:33 AM
15993 Reputation 2214 Total posts

Great to hear it, I'm closing this topic.

Regards

4
This is a demo site for MonoX. Please visit Mono Software for more info.