Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

How to get a list of Content Area Items in the order they are listed in the CMS

Vote:
 

Hi,

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

#211070
Dec 17, 2019 11:52
Vote:
 

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.

#211075
Dec 17, 2019 12:10
Vote:
 

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.

#211076
Edited, Dec 17, 2019 12:23
Vote:
 

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();
    }
#211116
Dec 17, 2019 20:59
Vote:
 

Cheers Mads! Exactly what I was looking for!

Its strange that Epi lets you re order CMS pages amongst Categories without the actual functionality to list them in that order on the front end

#211156
Edited, Dec 18, 2019 17:45
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.