Create blog posts

When creating a blog post, you must perform the following:

  1. Check whether the blog post already exists.

    Before you create the blog post, you must check whether a blog post with the same ID already exists.

  2. Create the blog post.

    If the blog post does not exist, you create a new blog post with the specified ID.

  3. Specify the parent blog of the blog post.

    Specify to which blog the blog post belongs.

  4. Set the required properties.

    When creating a new blog post, it is recommended to set at least the following properties:

    • Title
    • Content
    • LastModified
    • DateCreated
    • UrlName

    You can also set any other properties (e.g. Author, Summary, etc.) in this step.

  5. Save the blog post.

    Save all changes that you have made to the blog post.

  6. Publish the blog post.

    Finally, you publish the blog post using the workflow manager.

The example below shows you how to create blog post with predefined ID.

NOTE: The ID argument is assigned to the master version of the blog post. For more information about the different versions of a blog post, see For developers: Content lifecycle.

Creating a blog post with predefined ID

The following code creates a blog post with the specified ID, Title and Content. Native API ```C# using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using Telerik.Sitefinity.Blogs.Model; using Telerik.Sitefinity.Model; using Telerik.Sitefinity.Modules.Blogs; using Telerik.Sitefinity.Workflow;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Blogs { public partial class BlogPostsSnippets { private void CreateBlogPostNativeAPI(Guid masterBlogPostId, Guid parentBlogId, Lstring blogPostTitle, Lstring blogPostContent) { BlogsManager blogsManager = BlogsManager.GetManager(); BlogPost blogPost = blogsManager.GetBlogPosts().Where(item => item.Id == masterBlogPostId).FirstOrDefault();

        if (blogPost == null)
        {
            //The Blogs item is created as a master. The masterBlogPostId is assigned to the master.
            blogPost = blogsManager.CreateBlogPost(masterBlogPostId);

            //Set the parent blog.
            Blog blog = blogsManager.GetBlogs().Where(b => b.Id == parentBlogId).SingleOrDefault();
            blogPost.Parent = blog;

            //Set the properties of the blog post.                
            blogPost.Title = blogPostTitle;
            blogPost.Content = blogPostContent;
            blogPost.DateCreated = DateTime.UtcNow;
            blogPost.PublicationDate = DateTime.UtcNow;
            blogPost.LastModified = DateTime.UtcNow;
            blogPost.UrlName = Regex.Replace(blogPostTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");

            //Recompiles and validates the url of the blog.
            blogsManager.RecompileAndValidateUrls(blogPost);

            //Save the changes.
            blogsManager.SaveChanges();

            //Publish the Blogs item. The published version acquires new ID.
            var bag = new Dictionary<string, string>();
            bag.Add("ContentType", typeof(BlogPost).FullName);
            WorkflowManager.MessageWorkflow(masterBlogPostId, typeof(BlogPost), null, "Publish", false, bag);
        }
    }
}

}

First, you get an instance of the *BlogsManager* class. You check whether a blog post with the same *ID* already exists. To create the blog post, you call the CreateBlogPost method of the *BlogsManager* class. You can create a blog post with either predefined or auto-generated *ID* depending on which overload of the method you use. The method returns the master version of the blog post. Then, you set the properties of the master version. It is recommended to set at least the following properties: *Title*, *Content*, *UrlName*, *LastModified*, *PublicationDate*, *DateCreated*. To save the changes, you call the SaveChanges method of the manager. Finally, to publish the blog post, you call the MessageWorkflow method of the *WorkflowManager* class and pass the required parameters.
 **Fluent API**    ```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Telerik.Sitefinity.Blogs.Model;
using Telerik.Sitefinity.Workflow;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Blogs
{
    public partial class BlogPostsSnippets
    {
        private void CreateBlogPostFluentAPI(Guid masterBlogPostId, Guid parentBlogId, string blogPostTitle, string blogPostContent)
        {
            var count = 0;
            masterBlogPostId = Guid.NewGuid();

            //Check whether the parent blog exists.
            App.WorkWith().Blogs().Where(b => b.Id == parentBlogId).Count(out count);

            if (count > 0)
            {
                //The blog post is created as master. The masterBlogPostId is assigned to the master version.
                App.WorkWith().Blog(parentBlogId).CreateBlogPost()
                    //Set the properties of the blog post.
                .Do(blogPost =>
                {
                    //Set the properties.
                    masterBlogPostId = blogPost.Id;
                    blogPost.Title = blogPostTitle;
                    blogPost.DateCreated = DateTime.UtcNow;
                    blogPost.PublicationDate = DateTime.UtcNow;
                    blogPost.LastModified = DateTime.UtcNow;
                    blogPost.Content = blogPostContent;
                    blogPost.UrlName = Regex.Replace(blogPostTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                })
                //Save the changes.
                .Publish()    
                .SaveChanges();
            }
        }
    }
}

First, you check whether a blog post with the same ID already exists. Then, if it does not exist, you create the blog post by calling the CreateBlogPost method of the singular facade of the parent blog. The ID argument is assigned to the ID master version of the item. To set the properties of the blog post, you call the Do method of the facade. Then, you save the changes. Finally, to publish the blog post, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.