More in this section

Forums / Developing with Sitefinity / How to get Content Block from page

How to get Content Block from page

2 posts, 0 answered
  1. michael
    michael avatar
    4 posts
    01 Jun 2011
    12 Sep 2011
    Link to this post

    Am trying to write a routine that extracts content blocks from pages for the purposes of creating an XML file to feed another development.

    I am stuck on the last 1%. Below is the code I have so far:

    PageManager pManager = PageManager.GetManager();
    ContentManager contentmanager = ContentManager.GetManager();
    PageData page = null;
    IQueryable<PageNode> nodes = pManager.GetPageNodes().Where(t => t.ApprovalWorkflowState == "Published");
    foreach (PageNode node in nodes)
        crap = "";
        if (node.Page != null) { str += node.Page.Id + " - "; }
        str += node.Title + "<br />";
            if (node.Page != null)
                page = node.Page;
                //IList<PageControl> controls = page.Controls;
                List<ContentBlock> blocks = page.Controls.Where(c=>c.ObjectType==typeof(ContentBlock).FullName).Cast<ContentBlock>().ToList();
                foreach (ContentBlock block in blocks)
                    str += "<br /><br />" + block.Html + "<br /><br />";

    The issue I have is I successfully identify a PageControl as of type ContentBlock, but I cannot find a way to successfully cast or convert it to a ContentBlock. I have tried iterating through PageContol objects, above was my last attempt (which allowed me to compile) which was using "page.Controls.Where(c=>c.ObjectType==typeof(ContentBlock).FullName).Cast<ContentBlock>().ToList()" but it fails at runtime stating it cannot cast PageControl to ContentBlock.

    If anyone can help it would be much appreciated. If I can get the ContentBlock.Html value from the PageControl itself, I am not fussed, just need the data.

  2. Lubomir Velkov
    Lubomir Velkov avatar
    688 posts
    03 Nov 2014
    14 Sep 2011
    Link to this post
    Hi Michael,

    You should do the following - iterate through the Controls collection of a PageData and then use pManager.LoadControl() for each control and verify the returned result with

    var control = pManager.LoadControl(ctrl);
    if (control is ContentBlock)

    All the best,
    Lubomir Velkov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered