November Happy Hour will be moved to Thursday December 5th.

EPiServer CMS 7 :: Looping Through Blocks in Content Area

Vote:
 

Hi All,

I have a requirement where in

1) Want to loop through content area 

2) Find if it's a page dropped in content area or block

3) Find if any image is present in block

Thanks for help in advance.

#119811
Apr 03, 2015 10:06
Vote:
 

Something like this?

void DoSomething(ContentArea contentArea)
{
    if (contentArea != null && contentArea.FilteredItems.Any())
    {
        foreach (var item in contentArea.FilteredItems)
        {
            var content = item.GetContent();
            var block = content as BlockData;
            if (block != null)
            {
                // ...
                continue;
            }

            var page = content as PageData;
            if (page != null)
            {
                // ...
                continue;
            }

            // ...
        }
    }
}
#119812
Apr 03, 2015 10:18
Vote:
 

Yes something like this :)

Thanks Dejan, I'll try it out.

#119813
Apr 03, 2015 10:21
Vote:
 

In 7, Items changed to contents.

ContentArea contentArea = currentpage.ContentAreaForBlocks;
if (contentArea != null && contentArea.FilteredContents.Any())
{
foreach (var item in contentArea.FilteredContents)
{

But not getting item.GetContent()

#119814
Apr 03, 2015 10:24
Vote:
 

Is it 7.0?

Documentation says that contentArea.FilteredCotents already returns IContent, so there's no need to call item.GetContent() (I don't know in which version of EPi they introduced this extension :) )

#119815
Apr 03, 2015 10:33
Vote:
 

Yes it's 7.0

#119818
Apr 03, 2015 10:56
Vote:
 

@Dejan: How to find further that is there any image present in blocks?

E.g.: There are 3 blocks dragged and dropped in a content area and suppose 2nd blocks contains image, then I need to that image.

#119820
Apr 03, 2015 11:36
Vote:
 

Hi,

Sorry, I'm not sure what you mean.

If you drag & drop image into a content area, inside foreach loop, you can do this:

var imageFile = content as ImageData;
if (imageFile != null)
{
    ...       
}

Or, if you have an image property in block:

[ContentType(GUID = "5264a094-3a7d-41da-a191-c1ee3d9368e3")]
public class MyBlock : BlockData
{
    [Display(
        GroupName = SystemTabNames.Content,
        Order = 100)]
    [UIHint(UIHint.Image)]
    public virtual ContentReference MyImage { get; set; }
}

This is how you can check if MyImage is empty:

var myBlock = content as MyBlock;
if (myBlock != null)
{
    if (!ContentReference.IsNullOrEmpty(myBlock.MyImage))
    {
        // do something with image
    }
}

I'm not sure if this code works in 7.0

#119822
Apr 03, 2015 12:09
Vote:
 

In our case, image can be a string property, linkitem collection property in case of multiple images also.

E.g. There are three blocks

1) Block A

2) Block B

3) Block C

Now block B contains an image in a string property.

So I have to loop thru blocks and find which block contains image, in the above example block B.

What can be the way to find a block containing image?

#119823
Apr 03, 2015 12:20
Vote:
 

Can you show some code? How your block definition looks like?

#119824
Apr 03, 2015 12:37
Vote:
 

Just check if current item in the iteration through content area item is of type "Block B", if true - check for that string property and extract image if needed.

#119865
Apr 04, 2015 21:02
Vote:
 

Finally I managed to fulfill requirement as below:

I have total 4 blocks which contains image with either string/link item collection property.

So will create a switch case for those blocks and then match with the block type which is dragged and dropped in content area.

If matches any of them from switch case, then will read the image property directly by property name and get the work done.

Thanks Dejan & Valdis for help. :)

#119871
Apr 06, 2015 6:01
Vote:
 

Hi again,

If i write:

ContentArea contentArea = currentpage.ContentAreaForBlocks;
if (contentArea != null)
{
if (contentArea.Contents.Any())
{
foreach (var item in contentArea.Contents)
{
var block = item as BlockData;

}

}

}

Now suppose there's a block type created BT1 and a block created B1 of type BT1.

I get block name B1 from var block written in above code, but if i want its type BT1 then what will be its expression?

Thanks for the help in advance.

#119872
Apr 06, 2015 6:55
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.