Created
July 4, 2013 08:34
-
-
Save esteewhy/5925966 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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