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

Try our conversational search powered by Generative AI!

How to randomly select/pick any random item from ContentArea in Episerver

Vote:
 

I have added a number of blocks as contentArea Items in a ContentArea property.

I need to randomly pick a contentarea item and display it.

I don't want it in the order its mentioned in the contentarea

Please Help!!!

#224107
Jun 11, 2020 8:52
Vote:
 

Pick a random content area item:

public virtual ContentArea ContentArea { get; set; }

public ContentAreaItem RandomItem
{
    get
    {
        var random = new Random();
        return ContentArea?.FilteredItems.OrderBy(x => random.Next()).FirstOrDefault();
    }
}
#224108
Edited, Jun 11, 2020 9:13
Vote:
 

An alternative approach would be to make a Random Visitor Group and then personalising the blocks in the content area.  

#224149
Jun 12, 2020 9:26
Vote:
 

var random = new Random(unchecked((int)(DateTime.Now.Ticks)));
IList<ContentAreaItem> randomItemsList = contentAreaItemsList.OrderBy(t => random.Next(contentAreaItemsList.Count)).ToList();

#224150
Jun 12, 2020 9:35
Vote:
 

I thought you wanted to pick one content area item randomly.

Your own answer, that you accepted as the answer, returns all items in random order.

#224151
Jun 12, 2020 9:38
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.