Skip to content

Instantly share code, notes, and snippets.

@esteewhy
Created July 4, 2013 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esteewhy/5925966 to your computer and use it in GitHub Desktop.
Save esteewhy/5925966 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using Tridion.ContentManager.ContentManagement.Fields;
using Tridion.ContentManager.Templating;
using Tridion.ContentManager.Templating.Assembly;
using comm = Tridion.ContentManager.CommunicationManagement;
namespace Tridion.Training.Assemblies
{
[TcmTemplateTitle("Component Array")]
public class ComponentArray : ITemplate
{
public void Transform(Engine engine, Package package)
{
// Get the page object from the package
Item pageItem = package.GetByType(ContentType.Page);
// Get the content of the page using the page's TCMURI
comm.Page page = engine.GetObject(pageItem.GetAsSource().GetValue("ID")) as
comm.Page;
// Get items from parameters attached to the TBB
string m_ItemName = package.GetValue("itemName");
// Check if parameters are enmpty
if (String.IsNullOrEmpty(m_ItemName))
{
m_ItemName = "Components";
}
// check if page is not empty
if (page != null)
{
string fieldName = "heading";
var sortedList =
from cp in page.ComponentPresentations
let componentFields = new ItemFields(cp.Component.Content, cp.Component.Schema)
where componentFields.Contains(fieldName)
let textField = componentFields[fieldName] as TextField
where null != textField.Value
orderby textField.Value
select new ComponentPresentation(cp.Component.Id, cp.ComponentTemplate.Id);
var m_cpList = new ComponentPresentationList();
m_cpList.AddRange(sortedList);
// push component array into the package
package.PushItem(m_ItemName,
package.CreateStringItem(ContentType.ComponentArray, m_cpList.ToXml()));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment