MonoX support board

Start the conversation, ask questions and share tips and solutions with fellow developers.

Non-registered users can only browse through our support boards. Please register now if you want to post your questions. It takes a second and it is completely free. Alternatively, you can log in without registration using your credentials at major sites such as Google, Microsoft Live, OpenId, Facebook, LinkedIn or Yahoo.

Programmatically add post to Discussion Board (Closed) (Mono Support )

Viewed 17380 time(s), 4 post(s) 10/4/2011 3:44:35 PMby Maxim
Maxim

Maxim

10/4/2011 3:53:14 PM
Hi! How to programmatically add post to Discussion Board, and some comments to post...
This content has not been rated yet. 
319 Reputation 30 Total posts
khorvat

khorvat

10/5/2011 6:39:53 AM
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
Rated 5.00, 2 vote(s). 
15993 Reputation 2214 Total posts
Maxim

Maxim

10/5/2011 6:58:55 AM
Thank you,all works!
This content has not been rated yet. 
319 Reputation 30 Total posts
khorvat

khorvat

10/5/2011 7:09:33 AM
Great to hear it, I'm closing this topic.

Regards
This content has not been rated yet. 
15993 Reputation 2214 Total posts