Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.
Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.
Hi James,
Can you please provide the code sample here so that we can review it.
BTW You can use the below code to retrieve the items from contentArea.
var resultList = new List<IContent>();
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
foreach (ContentAreaItem contentAreaItem in currentPage.PrimaryComponentArea.FilteredItems)
{
if (contentAreaItem != null && contentAreaItem.ContentLink != null &&
contentAreaItem.ContentLink != ContentReference.EmptyReference)
{
IContent content;
if (contentLoader.TryGet(contentAreaItem.ContentLink, out content))
if (content != null)
{
resultList.Add(content);
}
}
}
FYI - Please get the IContentLoader context using constructor parameters.
Hi Ravindra,
So these items belong to a block and you can simply get the items by:
var items = block.LinkItems.Items.GetContentItems<IContentData>();
LinkItems is the collection of ContentArea items. GetContentItems uses the content loader to call GetItems. GetItems is the out of the box Epi method which is where I guess some sorting goes on to sort Categories first and then CMS pages.
James, you are right that the "GetItems" method does not return results in the same sequence as its input.
Its results are grouped per ContentProvider. Thats why you are seeing all Commerce categories before CMS pages.
I assume you are using a "GetContentItems" extension method similar to this.
You can use a modified "GetContentItems" extension method to get results in the original order:
public static IList<T> GetContentItems<T>(this IEnumerable<ContentAreaItem> contentAreaItems) where T : IContentData
{
if (contentAreaItems == null || !contentAreaItems.Any())
{
return null;
}
var contentLinks = contentAreaItems.Select(_ => _.ContentLink).ToList();
return ContentLoader.Value
.GetItems(contentLinks, new LoaderOptions { LanguageLoaderOption.FallbackWithMaster() })
.OfType<T>()
.OrderBy(x => contentLinks.IndexOf((x as IContent).ContentLink))
.ToList();
}
I have a list of content area items. The first item in the list is a Category and the second item is a CMS page and the rest of the items after that are all Categories.
From what I can see the out of the box Epi functionality around getting Content Area Items is that it looks at all Categories first and then CMS pages meaning the list is not in the order that they are listed in the back end. So what we get is all categories and then cms pages after.
Is there a way to get them to list in the correct order?
Thanks