Modify blogs

To modify a blog, you must perform the following:

  1. Get the blog.

    Get an instance of the blog that you want to modify.

  2. Modify the blog.

    Set the properties of the blog to the new values.

  3. Save the blog.

    Save the changes that have been made to the blog.

The example below shows you how to modify a blog by its ID.

Modifying a blog by its ID

The following code modifies a blog by its ID.

Native API

C#
using System;
using System.Linq;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Blogs.Model;
using Telerik.Sitefinity.Modules.Blogs;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Blogs
{
   public partial class BlogsSnippets
   {
       private void ModifyBlogNativeAPI(Guid blogId, string newTitle)
       {
           BlogsManager blogsManager = BlogsManager.GetManager();

           //Get the blog.
           Blog blog = blogsManager.GetBlogs().Where(b => b.Id == blogId).FirstOrDefault();

           if (blog != null)
           {
               //Modify the blog.
               blog.Title = newTitle;
               blog.LastModified = DateTime.UtcNow;
               blog.Urls.Clear();
               blog.UrlName = Regex.Replace(newTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");

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

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

First, you get an instance of the BlogsManager class. Then, you get the blog with the specified ID. You update the values of the properties of the blog. In this example you modify the title of the blog and its URL. Finally, you call SaveChanges to persist the changes.

Fluent API

C#
using System;
using System.Text.RegularExpressions;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Blogs
{
   public partial class BlogsSnippets
   {
       private void ModifyBlogFluentAPI(Guid blogId, string newTitle)
       {
           var count = 0;

           App.WorkWith().Blogs().Where(b => b.Id == blogId).Count(out count);

           if (count > 0)
           {
               //Get the blog.
               App.WorkWith().Blog(blogId).Do(blog =>
               {
                   //Modify the blog.
                   blog.Title = newTitle;
                   blog.LastModified = DateTime.UtcNow;
                   blog.Urls.Clear();
                   blog.UrlName = Regex.Replace(newTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
               }).SaveChanges();
           }
       }
   }
}

First, you check whether a blog with the specified ID exists. Then, you get the singular facade of the blog with the specified ID. To modify the blog, you call the Do method of the facade. In this example you update the title of the blog and its URL. Finally, to save the changes, you call the SaveChanges method of the facade.

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.
This Article Contains
New to Sitefinity?