I want to display a random quotation on each page of the website. If I try it using Content, it works but each module has its own content. When I switch to using Data, I can't seem to be able to randomly select an item in the list. I have a Content Type of 'Quotation' with just one field, called 'Quotation'.
Here is a test that works using Content:
<div>
<h2>@Dnn.Module.ModuleTitle</h2>
<div class="sc-element">Add more items to the list and see which one is selected at [email protected](ListContent)</div>
@{
Random rnd = new Random();
int r = rnd.Next(List.Count);
int rp1 = r + 1;
}
<div class="sc-element">Number @rp1, <i>@List[r].Content.Quotation</i>, was selected from this list of @List.Count items: </div>
<ol>
@foreach(var e in List) {
var Content = e.Content;
<li class="sc-element">
@Edit.Toolbar(Content)
@Content.Quotation
</li>
}
</ol>
If I try a similar thing with a Data stream, I can't access the list item randomly. Here's the code:
@using ToSic.Eav.DataSources
<div class="BasicContentWithPreview sc-element">
@Edit.Toolbar(ListContent)
<h2><span>Random: @Dnn.Module.ModuleTitle</span></h2>
@{
var allQuotes = CreateSource<EntityTypeFilter>();
allQuotes.TypeName = "Quotation";
}
@{
var ql = allQuotes.List;
Random rnd = new Random();
int r = rnd.Next(ql.Count);
int rp1 = r + 1;
}
<div class="sc-element">Number @rp1, <i>@AsDynamic(ql[r].Quotation)</i>, was selected from this list of @ql.Count items: </div>
<ol>
@foreach (var q in ql)
{
var item = AsDynamic(q.Value);
<li class="sc-element">[@item.EntityId] @item.Quotation</li>
}
</ol>
</div>
The error says:
'ToSic.Eav.IEntity' does not contain a definition for 'Quotation' and no extension method 'Quotation' accepting a first argument of type 'ToSic.Eav.IEntity' could be found
When I remove the ql[r]
section, it all works, and the list at the bottom is perfect. So I can access the item sequentially in the list, but cannot access them randomly. I've tried many combinations. Any ideas on how to do this properly? Thanks.
There is a Shuffle-Data Source starting in 2sxc 8.10 - http://2sxc.org/en/blog/post/releasing-2sxc-8-10-public-rest-api-visual-query-and-webapi-shuffle-datasource
You can use this in the visual query, or use a CreateSource<...> to use it. That should do it for you
You can also use a LINQ statement, for that you could look into the shuffle to see how it works https://github.com/2sic/eav-server/blob/master/ToSic.Eav.DataSources/Shuffle.cs
Hope this helped, if yes, please mark as answered :)