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; } // ... } } }
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()
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 :) )
@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.
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
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?
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.
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. :)
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.
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.