Create a page with a Blog posts widget

To create a standard page with a Blog posts widget added on it, perform the following:

  1. Create a standard Sitefinity CMS page.
    Perform procedure For developers: Create a page with the native API.
  2. Edit the page to add the Blog posts widget.
    You do this using the EditPage method of the PageManagerin the following way:
    1. SuppressSecurityChecks of the page provider.
    2. Get the page node by ID.
    3. Call the EditPage method.
  3. Create a new Blog post widget and add it to the page.
    Blog posts widget is represented by the BlogPostView class. Perform the following:
    1. Create an object of the BlogPostView class.
    2. Add the Blog posts widget control to the page draft and set its properties.
  4. Publish page draft and save changes
    Use the PublishPageDraft method of PageManager and call SaveChanges to finalize the creation of the page.

EXAMPLE: In the following code example, the CreatePageWithBlogPostWidget method creates a new standard page and adds the Blog posts widget on it:

C#
using System;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.Services;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Pages
{
   public partial class PagesSnippets
   {
       public void CreatePageWithBlogPostWidget(string pageName)
       {
           //creates a standard page
           PageManager pageManager = PageManager.GetManager();
           var parentPageNodeId = SiteInitializer.CurrentFrontendRootNodeId;

           PageNode parent = pageManager.GetPageNode(parentPageNodeId);
           var pageId = Guid.NewGuid();

           PageNode pageNode = pageManager.CreatePage(parent, pageId, NodeType.Standard);

           PageData pageData = pageNode.GetPageData();

           pageData.HtmlTitle = pageName;

           pageNode.Name = pageName;
           pageNode.Description = pageName;
           pageNode.Title = pageName;
           pageNode.ShowInNavigation = true;
           pageNode.ApprovalWorkflowState = "Published";

           pageManager.SaveChanges();

           //adds the blog post widget
           pageManager.Provider.SuppressSecurityChecks = true;
           var pageDataId = pageManager.GetPageNode(pageId).PageId;
           var page = pageManager.EditPage(pageDataId, culture);

           var blogsWidget = new Telerik.Sitefinity.Modules.Blogs.Web.UI.BlogPostView();

           var blogsWidgetControl = pageManager.CreateControl<PageDraftControl>(blogsWidget, "Body");
           blogsWidgetControl.Caption = "BlogPostWidget";
           pageManager.SetControlDefaultPermissions(blogsWidgetControl);
           page.Controls.Add(blogsWidgetControl);

           //publishes draft page
           pageManager.PublishPageDraft(page, culture);
           pageManager.SaveChanges();
       }
   }
}
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.
New to Sitefinity?