---
title: Blogs
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

List of examples:

## Blogs

### Creating a new blog

```csharp

// Prepares a TreeProvider instance
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets a parent page under which the blog will be created
TreeNode parentPage = tree.SelectNodes()
    .Path("/Blogs")
    .OnCurrentSite()
    .Culture("en-us")
    .TopN(1)
    .FirstOrDefault();

if (parentPage != null)
{
    // Creates a new blog page
    TreeNode blogPage = TreeNode.New("CMS.Blog", tree);

    // Sets the basic page properties for the blog
    blogPage.DocumentName = "NewBlog";
    blogPage.DocumentCulture = "en-us";

    // Sets values for the fields of the blog page type
    blogPage.SetValue("BlogName", "NewBlog");
    blogPage.SetValue("BlogDescription", "This blog was created through the API.");
    blogPage.SetValue("BlogOpenCommentsFor", "##ALL##");
    blogPage.SetValue("BlogAllowAnonymousComments", "true");
    blogPage.SetValue("BlogModerateComments", "");
    blogPage.SetValue("BlogOpenCommentsFor", "");
    blogPage.SetValue("BlogUseCAPTCHAForComments", "");
    blogPage.SetValue("BlogEnableSubscriptions", "true");

    // Gets a page template for the blog page
    PageTemplateInfo pageTemplate = PageTemplateInfoProvider.GetPageTemplateInfo("CorporateSite.BlogDetail");
    if (pageTemplate != null)
    {
        // Assigns the page template to the blog
        blogPage.SetDefaultPageTemplateID(pageTemplate.PageTemplateId);
    }

    // Inserts the blog under the specified parent page
    blogPage.Insert(parentPage);
}

```

[> Back to list of examples](#blogs-toc)

### Updating a blog

```csharp

// Prepares a TreeProvider instance
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets the blog page
TreeNode blogPage = tree.SelectNodes()
    .Path("/Blogs/NewBlog")
    .OnCurrentSite()
    .Culture("en-us")
    .TopN(1)
    .FirstOrDefault();

if (blogPage != null)
{
    // Changes the name of the blog page
    blogPage.DocumentName = blogPage.DocumentName.ToLower();

    // Saves the updated blog to the database
    blogPage.Update();
}

```

[> Back to list of examples](#blogs-toc)

### Updating multiple blogs

```csharp

// Prepares a TreeProvider instance
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets all blog pages under the current site's "/Blogs" section
var blogPages = tree.SelectNodes("CMS.Blog")
                                .OnCurrentSite()
                                .Path("/Blogs", PathTypeEnum.Children);

// Loops through individual blog pages
foreach (TreeNode blogPage in blogPages)
{
    // Changes the name of the blog page
    blogPage.DocumentName = blogPage.DocumentName.ToUpper();

    // Saves the updated blog to the database
    blogPage.Update();
}

```

[> Back to list of examples](#blogs-toc)

### Deleting a blog

```csharp

// Prepares a TreeProvider instance
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets the blog page
TreeNode blogPage = tree.SelectNodes()
    .Path("/Blogs/NewBlog")
    .OnCurrentSite()
    .Culture("en-us")
    .TopN(1)
    .FirstOrDefault();

if (blogPage != null)
{
    // Deletes the blog (all language versions)
    blogPage.DeleteAllCultures();
}

```

[> Back to list of examples](#blogs-toc)

## Blog posts

### Creating a blog post

```csharp

// Prepares a TreeProvider instance
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets a parent blog page
TreeNode parentBlog = tree.SelectNodes()
    .Path("/Blogs/NewBlog")
    .OnCurrentSite()
    .Culture("en-us")
    .TopN(1)
    .FirstOrDefault();

if (parentBlog != null)
{
    // Creates a new blog post page
    TreeNode blogPost = TreeNode.New("CMS.BlogPost", tree);

    // Sets the page properties and blog post field values
    blogPost.DocumentName = "NewBlogPost";
    blogPost.SetValue("BlogPostTitle", "NewBlogPost");
    blogPost.SetValue("BlogPostDate", DateTime.Now);
    blogPost.SetValue("BlogPostSummary", "Blog post summary");
    blogPost.SetValue("BlogPostBody", "Blog post body");
    blogPost.SetValue("BlogPostAllowComments", "true");
    blogPost.DocumentCulture = "en-us";

    // Prepares a blog month page as the parent for the post under the specified blog
    TreeNode parentMonth = DocumentHelper.EnsureBlogPostHierarchy(blogPost, parentBlog, tree);

    // Creates the blog post
    blogPost.Insert(parentMonth);
}

```

[> Back to list of examples](#blogs-toc)

### Updating blog posts

```csharp

// Prepares a TreeProvider instance
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets all published blog posts under "/Blogs/NewBlog" as a DataSet
DataSet posts = BlogHelper.GetBlogPosts(SiteContext.CurrentSiteName, "/Blogs/NewBlog", null, true, null, null, true);

if (!DataHelper.DataSourceIsEmpty(posts))
{
    // Loops through individual blog posts in the DataSet
    foreach (DataRow postDr in posts.Tables[0].Rows)
    {
        // Creates a page object from the DataRow
        TreeNode modifyPost = TreeNode.New("cms.blogpost", postDr, tree);

        // Updates the blog post properties
        modifyPost.SetValue("BlogPostBody", "The blog post body was updated.");

        // Saves the updated blog post to the database
        modifyPost.Update();
    }
}

```

[> Back to list of examples](#blogs-toc)

### Deleting a blog post

```csharp

// Prepares a TreeProvider instance
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets all published blog posts under "/Blogs/NewBlog" as a DataSet
DataSet posts = BlogHelper.GetBlogPosts(SiteContext.CurrentSiteName, "/Blogs/NewBlog", null, true, null, null, true);

if (!DataHelper.DataSourceIsEmpty(posts))
{
    // Gets the first blog post page from the DataSet
    TreeNode deletePost = TreeNode.New("cms.blogpost", posts.Tables[0].Rows[0], tree);

    // Deletes the blog post (all language versions)
    deletePost.DeleteAllCultures();
}

```

[> Back to list of examples](#blogs-toc)

## Blog comments

### Creating a blog comment

```csharp

// Prepares the blog post object
TreeNode blogPost = null;

// Gets all published blog posts under "/Blogs/NewBlog" as a DataSet
DataSet posts = BlogHelper.GetBlogPosts(SiteContext.CurrentSiteName, "/Blogs/NewBlog", null, true, null, null, true);

if (!DataHelper.DataSourceIsEmpty(posts))
{
    // Prepares a TreeProvider instance
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

    // Gets the first blog post from the DataSet
    blogPost = TreeNode.New("cms.blogpost", posts.Tables[0].Rows[0], tree);
}

if (blogPost != null)
{
    // Creates a new blog comment object
    BlogCommentInfo newComment = new BlogCommentInfo();

    // Sets the comment properties
    newComment.CommentText = "New comment";
    newComment.CommentUserName = MembershipContext.AuthenticatedUser.UserName;
    newComment.CommentUserID = MembershipContext.AuthenticatedUser.UserID;
    newComment.CommentApprovedByUserID = MembershipContext.AuthenticatedUser.UserID;
    newComment.CommentPostDocumentID = blogPost.DocumentID;
    newComment.CommentDate = DateTime.Now;

    // Saves the blog comment to the database
    BlogCommentInfoProvider.SetBlogCommentInfo(newComment);
}

```

[> Back to list of examples](#blogs-toc)

### Updating blog comments

```csharp

// Prepares the blog post object
TreeNode blogPost = null;

// Gets all published blog posts under "/Blogs/NewBlog" as a DataSet
DataSet posts = BlogHelper.GetBlogPosts(SiteContext.CurrentSiteName, "/Blogs/NewBlog", null, true, null, null, true);

if (!DataHelper.DataSourceIsEmpty(posts))
{
    // Prepares a TreeProvider instance
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

    // Gets the first blog post from the DataSet
    blogPost = TreeNode.New("cms.blogpost", posts.Tables[0].Rows[0], tree);
}

if (blogPost != null)
{
    // Gets all comments under the specified blog post that are not marked as spam
    var blogComments = BlogCommentInfoProvider.GetBlogComments()
                                                    .WhereEquals("CommentPostDocumentID", blogPost.DocumentID)
                                                    .WhereEquals("CommentIsSpam", 0);

    // Loops through individual blog comments
    foreach (BlogCommentInfo blogComment in blogComments)
    {
        // Updates the comment text
        blogComment.CommentText = blogComment.CommentText.ToUpper();

        // Saves the updated blog comment to the database
        BlogCommentInfoProvider.SetBlogCommentInfo(blogComment);
    }
}

```

[> Back to list of examples](#blogs-toc)

### Deleting blog comments

```csharp

// Prepares the blog post object
TreeNode blogPost = null;

// Gets all published blog posts under "/Blogs/NewBlog" as a DataSet
DataSet posts = BlogHelper.GetBlogPosts(SiteContext.CurrentSiteName, "/Blogs/NewBlog", null, true, null, null, true);

if (!DataHelper.DataSourceIsEmpty(posts))
{
    // Prepares a TreeProvider instance
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

    // Gets the first blog post from the DataSet
    blogPost = TreeNode.New("cms.blogpost", posts.Tables[0].Rows[0], tree);
}

if (blogPost != null)
{
    // Gets all comments under the specified blog post that are marked as spam
    var blogComments = BlogCommentInfoProvider.GetBlogComments()
                                                    .WhereEquals("CommentPostDocumentID", blogPost.DocumentID)
                                                    .WhereEquals("CommentIsSpam", 1);

    // Loops through individual blog comments
    foreach (BlogCommentInfo blogComment in blogComments)
    {
        // Deletes the blog comment
        BlogCommentInfoProvider.DeleteBlogCommentInfo(blogComment);
    }
}

```

[> Back to list of examples](#blogs-toc)

## Blog post subscriptions

### Creating a blog post subscription

```csharp

// Prepares the blog post object
TreeNode blogPost = null;

// Gets all published blog posts under "/Blogs/NewBlog" as a DataSet
DataSet posts = BlogHelper.GetBlogPosts(SiteContext.CurrentSiteName, "/Blogs/NewBlog", null, true, null, null, true);

if (!DataHelper.DataSourceIsEmpty(posts))
{
    // Prepares a TreeProvider instance
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

    // Gets the first blog post from the DataSet
    blogPost = TreeNode.New("cms.blogpost", posts.Tables[0].Rows[0], tree);
}

if (blogPost != null)
{
    // Creates a new blog post subscription object
    BlogPostSubscriptionInfo newSubscription = new BlogPostSubscriptionInfo();

    // Sets the subscription properties (subscribes the current user)
    newSubscription.SubscriptionPostDocumentID = blogPost.DocumentID;
    newSubscription.SubscriptionUserID = MembershipContext.AuthenticatedUser.UserID;
    newSubscription.SubscriptionEmail = MembershipContext.AuthenticatedUser.Email;

    // Saves the blog post subscription to the database
    BlogPostSubscriptionInfoProvider.SetBlogPostSubscriptionInfo(newSubscription);
}

```

[> Back to list of examples](#blogs-toc)

### Updating a blog post subscription

```csharp

// Prepares the blog post object
TreeNode blogPost = null;

// Gets all published blog posts under "/Blogs/NewBlog" as a DataSet
DataSet posts = BlogHelper.GetBlogPosts(SiteContext.CurrentSiteName, "/Blogs/NewBlog", null, true, null, null, true);

if (!DataHelper.DataSourceIsEmpty(posts))
{
    // Prepares a TreeProvider instance
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

    // Gets the first blog post from the DataSet
    blogPost = TreeNode.New("cms.blogpost", posts.Tables[0].Rows[0], tree);
}

if (blogPost != null)
{
    // Gets the subscription for the specified blog post and email address
    BlogPostSubscriptionInfo updateSubscription = BlogPostSubscriptionInfoProvider.GetBlogPostSubscriptionInfo("administrator@localhost.local", blogPost.DocumentID);

    if (updateSubscription != null)
    {
        // Updates the subscription properties
        updateSubscription.SubscriptionApproved = true;

        // Saves the updated blog post subscription to the database
        BlogPostSubscriptionInfoProvider.SetBlogPostSubscriptionInfo(updateSubscription);
    }
}

```

[> Back to list of examples](#blogs-toc)

### Updating multiple blog post subscriptions

```csharp

// Gets all blog post subscriptions in the system whose email address ends with 'localhost.local'
var subscriptions = BlogPostSubscriptionInfoProvider.GetBlogPostSubscriptions().WhereEndsWith("SubscriptionEmail", "localhost.local");

// Loops through individual subscriptions
foreach (BlogPostSubscriptionInfo subscription in subscriptions)
{
    // Updates the subscription properties
    subscription.SubscriptionApproved = false;

    // Saves the updated blog post subscription to the database
    BlogPostSubscriptionInfoProvider.SetBlogPostSubscriptionInfo(subscription);
}

```

[> Back to list of examples](#blogs-toc)

### Deleting a blog post subscription

```csharp

// Prepares the blog post object
TreeNode blogPost = null;

// Gets all published blog posts under "/Blogs/NewBlog" as a DataSet
DataSet posts = BlogHelper.GetBlogPosts(SiteContext.CurrentSiteName, "/Blogs/NewBlog", null, true, null, null, true);

if (!DataHelper.DataSourceIsEmpty(posts))
{
    // Prepares a TreeProvider instance
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

    // Gets the first blog post from the DataSet
    blogPost = TreeNode.New("cms.blogpost", posts.Tables[0].Rows[0], tree);
}

if (blogPost != null)
{
    // Gets the subscription for the specified blog post and email address
    BlogPostSubscriptionInfo deleteSubscription = BlogPostSubscriptionInfoProvider.GetBlogPostSubscriptionInfo("administrator@localhost.local", blogPost.DocumentID);

    if (deleteSubscription != null)
    {
        // Deletes the blog post subscription
        BlogPostSubscriptionInfoProvider.DeleteBlogPostSubscriptionInfo(deleteSubscription);
    }
}

```

[> Back to list of examples](#blogs-toc)
