Creating content block with shared content
Sitefinity allows you to create a content block control that uses shared content item.
For more information about creating content items, see Creating content items.
The following code creates a content block with shared content and adds it to a page:
ContentItem contentItem = GetContentItemById(new Guid(SharedContentItemId));
ContentManager manager = ContentManager.GetManager();
contentItem = manager.GetLive(contentItem);
ContentBlock sharedContentBlock = new ContentBlock();
if (contentItem != null)
{
sharedContentBlock.SharedContentID = contentItem.Id;
sharedContentBlock.Html = contentItem.Content;
}
AddControlToPage(new Guid(PageId), sharedContentBlock, placeHolder, caption);
First, you query for the content item with the specified Id using GetContentItemById. The method GetContentItemById is defined in Querying content items.
Then, you initialize the ContentManager. To get the live version of the content item, you use the GetLive method of the manager. To define the shared content item that the content block uses, you must set the SharedContentID property. To set the initial content of the content block, you use the Html property.
NOTE: The content in the content block and in the content item is synchronized. When you modify the content in one of the controls, the other modifies also.
Finally, you add the control to the page with the specified Id, placeholder and caption. To add the control, you use AddControlToPage. The method AddControlToPage is defined in Adding and removing controls.
See Also