Create a Content block widget that displays shared content

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 Create content blocks and Content block widget.

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

C#
using System;
using System.Linq;
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(string pageTitle)
       {
           // get an instance of the contentManager
           var contentManager = ContentManager.GetManager();

           // create new content block
           var contentItem = CreateContentItem(contentManager);

           // get a PageManager object
           PageManager manager = PageManager.GetManager();

           // get the page
           var pageData = manager.GetPageDataList().Where(pD => pD.HtmlTitle == pageTitle).FirstOrDefault();

           // 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;

           // create and add the control
           var draftControl = manager.CreateControl<PageDraftControl>(contentBlock, "Body");
           manager.SetControlDefaultPermissions(draftControl);
           draftPage.Controls.Add(draftControl);

           // 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";
           contentItem.Content = contentItem.Title;
           contentManager.SaveChanges();

           contentManager.Lifecycle.Publish(contentItem);

           contentManager.SaveChanges();
           return contentItem;
       }
   }
}

First, you create the content block using CreateContent method of the content manager. Then, you initialize the PageManager. You create new page data by using LINQ to get an existing page or create a new one using the GetPageDataList 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?
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?