How to get Content Block from page

Posted by Community Admin on 04-Aug-2018 12:13

How to get Content Block from page

All Replies

Posted by Community Admin on 12-Sep-2011 00:00

Hi

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.

Thanks

Posted by Community Admin on 14-Sep-2011 00:00

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

This thread is closed