Managing blogs using Kentico CMS API

Since the blog posts are standard Kentico CMS documents, you can create, modify, read and delete them using standard API for documents. You can find more details and examples in chapter Content management internals and the subsequent chapters.

 

The following examples show how you can work with comments using the API:

 

Adding a new comment

 

[C#]

 

using CMS.CMSHelper;

using CMS.GlobalHelper;

using CMS.TreeEngine;

using CMS.SiteProvider;

using CMS.Blogs;

 

...

 

           // Use a valid alias path of a testing post

           string postAlias = "/TestingBlog/October-2007/TestingPost";

 

           // Prepare the TreeProvider (it must be initialized with user information when editing

           // document structure)

           UserInfo ui = UserInfoProvider.GetUserInfo("administrator");

           TreeProvider tree = new TreeProvider(ui);

 

           CMS.TreeEngine.TreeNode postNode = tree.SelectSingleNode(CMSContext.CurrentSiteName,

              postAlias, TreeProvider.ALL_CULTURES, true, null, false);

           if (postNode != null)

           {

              // Add new comment to the post

              BlogCommentInfo commentObj = new BlogCommentInfo();

              commentObj.CommentDate = DateTime.Now;

              commentObj.CommentPostDocumentID = postNode.DocumentID;

              commentObj.CommentText = "Some comment text";

              commentObj.CommentUserID = ui.UserID;

              commentObj.CommentUserName = (ui.UserNickName != "") ? ui.UserNickName : ui.FullName;

 

             // Save comment

             BlogCommentInfoProvider.SetBlogCommentInfo(commentObj);

              }

 

 

Editing an existing comment

 

[C#]

 

using CMS.CMSHelper;

using CMS.GlobalHelper;

using CMS.TreeEngine;

using CMS.SiteProvider;

using CMS.Blogs;

 

...

 

           // Use a valid alias path of a testing post

           string postAlias = "/TestingBlog/October-2007/TestingPost";

 

           // Prepare the TreeProvider (it must be initialized with user information when editing

           // document structure)

           UserInfo ui = UserInfoProvider.GetUserInfo("administrator");

           TreeProvider tree = new TreeProvider(ui);

           DataSet ds = null;

 

           // Get the parent post document

           CMS.TreeEngine.TreeNode postNode = tree.SelectSingleNode(CMSContext.CurrentSiteName, postAlias, TreeProvider.ALL_CULTURES, true, null, false);

           if (postNode != null)

           {

               // Get all post comments

               ds = BlogCommentInfoProvider.GetPostComments(postNode.DocumentID, false);

               if (!DataHelper.DataSourceIsEmpty(ds))

               {

                   // Set all post comments as approved

                   foreach (DataRow dr in ds.Tables[0].Rows)

                   {

                       // Edit comment and save it

                       BlogCommentInfo commentObj = new BlogCommentInfo(dr);

                       commentObj.CommentApprovedByUserID = ui.UserID;

                       BlogCommentInfoProvider.SetBlogCommentInfo(commentObj);

                   }

               }

           }

 

 

Deleting a comment

 

[C#]

 

using CMS.CMSHelper;

using CMS.GlobalHelper;

using CMS.TreeEngine;

using CMS.SiteProvider;

using CMS.Blogs;

 

...

 

       // Use a valid alias path of a testing post

       string postAlias = "/TestingBlog/October-2007/TestingPost";

 

       // Prepare the TreeProvider (it must be initialized with user information when editing

       // document structure)

       UserInfo ui = UserInfoProvider.GetUserInfo("administrator");

       TreeProvider tree = new TreeProvider(ui);

       DataSet ds = null;

 

 

       // Get the parent post document

       CMS.TreeEngine.TreeNode postNode = tree.SelectSingleNode(CMSContext.CurrentSiteName,

           postAlias, TreeProvider.ALL_CULTURES, true, null, false);

       if (postNode != null)

       {

           // Get all post comments

           ds = BlogCommentInfoProvider.GetPostComments(postNode.DocumentID, false);

           if (!DataHelper.DataSourceIsEmpty(ds))

           {

              // Delete all post comments

              foreach (DataRow dr in ds.Tables[0].Rows)

              {

                  // Delete comment

                  BlogCommentInfo commentObj = new BlogCommentInfo(dr);

                  BlogCommentInfoProvider.DeleteBlogCommentInfo(commentObj.CommentID);

              }

           }

       }