I have a page template that uses a following template building block to extract components from a page.

<!-- TemplateBeginRepeat name="Components" --> 
<!-- TemplateEndRepeat -->

I want to write same sort of code in C sharp.

            var session = engine.GetSession();
            var uri = package.GetValue("Component.ID");
            Page page = (Page)session.GetObject(package.GetValue("Page.ID"));
            foreach (var cp in page.ComponentPresentations)

Can some help me to proceed further.


The following c# TBB provides the same functional result as the DWT sample you include.

[TcmTemplateTitle("Render All Component Presentations")]
public class RenderAllComponentPresentations : ITemplate
    public void Transform(Engine engine, Package package)
        Page page = (Page)engine.GetObject(package.GetByName(Package.PageName));
        StringBuilder output = new StringBuilder();
        foreach (CP cp in page.ComponentPresentations)
            output.Append(engine.RenderComponentPresentation(cp.Component.Id, cp.ComponentTemplate.Id));
        package.PushItem(Package.OutputName, package.CreateStringItem(ContentType.Html, output.ToString()));
  • Thank you for answer. I was actually searching for sample page TBB.
    – Patan
    Nov 25 '13 at 15:05
  • 1
    Was this answer helpful as a sample page TBB, or were you looking for something else? It looks like a good page-level example to me, but maybe I'm missing something. :-)
    – Alvin Reyes
    Nov 26 '13 at 5:40

You can simply use Tridion.ContentManager.Templating.RenderComponentPresentation()

public virtual string RenderComponentPresentation(
    TcmUri componentUri,
    TcmUri componentTemplateUri

This is exposed by the Engine class, so in a template it would be engine.RenderComponentPresentation()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.