Kentico CMS 7.0 Developer's Guide

Managing forum posts

Managing forum posts

Previous topic Next topic Mail us feedback on this topic!  

Managing forum posts

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example creates a forum post.

 

private bool CreateForumPost()
{

  // Get the forum
  ForumInfo forum = ForumInfoProvider.GetForumInfo("MyNewForum", CMSContext.CurrentSiteID);

    if (forum != null)
    {
        // Create new forum post object
        ForumPostInfo newPost = new ForumPostInfo();
 
        // Set the properties
        newPost.PostUserID = CMSContext.CurrentUser.UserID;
        newPost.PostUserMail = CMSContext.CurrentUser.Email;
        newPost.PostUserName = CMSContext.CurrentUser.UserName;
        newPost.PostForumID = forum.ForumID;
        newPost.PostTime = DateTime.Now;
        newPost.PostApproved = true;
        newPost.PostText = "This is my new post";
        newPost.PostSubject = "My new post";
 
        // Save the forum post
        ForumPostInfoProvider.SetForumPostInfo(newPost);
 
        return true;
    }
 
    return false;
}

 

The following example gets and updates a forum post.

 

private bool GetAndUpdateForumPost()
{
    // Prepare the parameters
    string where = "PostSubject LIKE N'My new post%'";
    string orderBy = "";
    string columns = "";
    int topN = 10;

 

  // Get the data
  DataSet posts = ForumPostInfoProvider.GetForumPosts(where, orderBy, topN, columns);

    if (!DataHelper.DataSourceIsEmpty(posts))
    {
        ForumPostInfo updatePost = new ForumPostInfo(posts.Tables[0].Rows[0]);
 
        // Update the properties
        updatePost.PostSubject = updatePost.PostSubject.ToLower();
 
        // Save the changes
        ForumPostInfoProvider.SetForumPostInfo(updatePost);
 
        return true;
    }
 
    return false;
}

 

The following example gets and bulk updates forum posts.

 

private bool GetAndBulkUpdateForumPosts()
{
    // Prepare the parameters
    string where = "PostSubject LIKE N'My new post%'";
    string orderBy = "";
    string columns = "";
    int topN = 10;

 
  // Get the data
  DataSet posts = ForumPostInfoProvider.GetForumPosts(where, orderBy, topN, columns);

    if (!DataHelper.DataSourceIsEmpty(posts)
    {
        // Loop through the individual items
        foreach (DataRow postDr in posts.Tables[0].Rows)
 
        {
            // Create object from DataRow
            ForumPostInfo modifyPost = new ForumPostInfo(postDr);
 
            // Update the properties
            modifyPost.PostSubject = modifyPost.PostSubject.ToUpper();
 
            // Save the changes
            ForumPostInfoProvider.SetForumPostInfo(modifyPost);
        }
 
        return true;
    }
 
    return false;
}

 

The following example deletes a forum post.

 

private bool DeleteForumPost()
{
    // Prepare the parameters
    string where = "PostSubject LIKE N'My new post%'";
    string orderBy = "";
    string columns = "";
    int topN = 10;

 
  // Get the data
  DataSet posts = ForumPostInfoProvider.GetForumPosts(where, orderBy, topN, columns);

    if (!DataHelper.DataSourceIsEmpty(posts))
    {
        // Get the forum post
        ForumPostInfo deletePost = new ForumPostInfo(posts.Tables[0].Rows[0]);
 
        // Delete the forum post
        ForumPostInfoProvider.DeleteForumPostInfo(deletePost);
 
        return true;
    }
 
    return false;
}