Data FAQ

How to add content to the placeholders?

How to add content to the placeholders?

Answer:

To add information to the content placeholder use the following C# function:

(Prior to adding this function, please add the reference to System.Transactions).

using System;
using System.Linq;
using Composite.Data;
using Composite.Data.Types;
using Composite.Data.Transactions;

namespace Composite.Tools.Installation
{
	public class NewPlaceHolder
	{
		public static void CreatePlaceHolder(Guid gPageGuid, string sPlaceHolderId, string sContent)
		{
			using (System.Transactions.TransactionScope transactionScope = TransactionsFacade.CreateNewScope())
			{
				using (DataScope dataScope = new DataScope(DataScopeIdentifier.Administrated))
				{
                    using (DataConnection connection = new DataConnection())
                    {
                        IPagePlaceholderContent context = DataConnection.New<IPagePlaceholderContent>();
                        context.PageId = gPageGuid;
                        context.PlaceHolderId = sPlaceHolderId;
                        context.Content = sContent;
                        context.PublicationStatus = "draft";
                        IPagePlaceholderContent createdPagePlaceHolder = connection.Add<IPagePlaceholderContent>(context);
                    }
				}
				transactionScope.Complete();
			}
		}
	}
}

Download the source

Note: This function adds content in HTML format (sContent) to the selected content placeholder (sPlaceHolderId) of the selected page (gPageGuid).

Make sure not to exceed the amount of the placeholders in the template used to create this page. Add the same number of placeholders that the template of this page contains.

Additionally pay attention to the status of the placeholders. Keep it synchronized (all placeholders that belong to the same page have to be with the same status).

sContent, sPlaceHolders, gPageGuid can be found in:

  • SQL Provider: [Composite_Data_Types_IPagePlaceholderContent_administrated] table
  • XML Provider:\App_Data\Composite\DataStores\Composite.Data.Types.IPagePlaceholderContents_administrated.xml file

Here is the sample how to call such function:

NewPlaceHolder.CreatePlaceHolder(gPage, sPlaceHolderId, "<hr/>");
Where:

  • gPage is the GUID of the page that placeholders are being added to
  • sPlaceHolderId is ID of the rendering:placeholder tag in the template XML file
  • and the last parameter is the content itself