Create a Content block widget that displays shared content

NEW TO SITEFINITY?

You can create and add to a page a Content block widget that displays a shared content block.

By default, content blocks are shared content and you can display one block on many pages, using the Content block widget. Sitefinity CMS automatically synchronizes the content in the content block and in the Content block widget. When you modify the content in the widget or in the module, Sitefinity CMS applies the changes everywhere where the content block is used. For more information, see For developers: Create content blocks and Content block widget.

The following code creates a content block with shared content and adds it to a page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Modules.GenericContent;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.ContentBlocks
{
public partial class ContentBlocksSnippets
{
public static void CreatePageWithSharedContentItemSnippet()
{
//gets an instance of the contentManager
var contentManager = ContentManager.GetManager();
//creates new content block
var contentItem = CreateContentItem(contentManager);
//gets a PageManager object
PageManager manager = PageManager.GetManager();
//creates new page
var pageData = CreatePageData(manager, Guid.NewGuid());
//edit the page to add content block
var draftPage = manager.EditPage(pageData.Id);
var contentBlock = new Telerik.Sitefinity.Modules.GenericContent.Web.UI.ContentBlock();
contentBlock.Html = contentItem.Content.Value;
contentBlock.SharedContentID = contentItem.Id;
var draftControl = manager.CreateControl<PageDraftControl>(contentBlock, "Body");
draftPage.Controls.Add(draftControl);
//Create a node in the SiteMap for that page.
PageNode node = manager.CreatePageNode();
var parent = manager.GetPageNode(SiteInitializer.CurrentFrontendRootNodeId);
manager.ChangeParent(node, parent);
//set the node properties
node.Title = "page title";
node.ShowInNavigation = true;
node.UrlName = "url";
//associate the node with the PageData object
//valid before 7.0
//node.Page = pageData;
//Save the changes
manager.PublishPageDraft(draftPage);
manager.SaveChanges();
}
private static ContentItem CreateContentItem(ContentManager contentManager)
{
var contentItem = contentManager.CreateContent(Guid.NewGuid());
contentItem.Title = "content item title";
contentItem.UrlName = "content-item-url-name";
contentManager.SaveChanges();
//valid before 7.0
//contentItem = contentManager.Publish(contentItem);
//since version 7.0 you must use the Lifecycle property
contentManager.Lifecycle.Publish(contentItem);
contentManager.SaveChanges();
return contentItem;
}
private static PageData CreatePageData(PageManager pageManager, Guid id)
{
//It holds the actual page contents
PageData pageData = pageManager.CreatePageData(id);
pageData.Template = pageManager.GetTemplates().First();
pageData.HtmlTitle = "page title";
//valid before 7.0
//pageData.Title = "page title";
//since 7.0 some of the PageData properties are moved to the PageNode
//check API Changes in Sitefinity 7.0
pageData.NavigationNode.Title = "page title";
//Set the page status as published. The default status is draft
pageData.Status = ContentLifecycleStatus.Live;
return pageData;
}
}
}

First, you create the content block using CreateContent method of the content manager. Then, you initialize the PageManager. You create new page data using the CreatePageData method of the PageManager. For more information on creating page data, see For developers: Create pages. You edit the page to add the Content block widget. Finally, you call SaveChanges.

Want to learn more?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?