I have the requirement to show a random review on a client's website. I am saving the reviews fine using Umbraco Forms (V4.0.1) however I can't quite work out how to retrieve a random approved entry. The code I am using to retrieve the recordlist is as follows:
var recordlist = Library.GetApprovedRecordsFromForm("30769e6f-9de0-4365-b67f-5964876dc0f0").Items.Where(x => x.State.ToString() == "Approved");
Can anyone tell me what the best way is to retrieve a random item from the list of items retrieved above?
As an aside, the reason I am doing an additional check on the record state is that GetApprovedRecordsFromForm is returning all records, even ones that are not approved.
I will give that a try thanks. I presume that this just method just randomizes the items within the list and then you can just select the first item which should or should not be different when the method is next called?
Perhaps you need to do it in the foreach loop, but give it a try and see if it works. You can see the different methods here in the cheatsheets https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets. (PDF files). There are both cheatsheets for both dynamic Razor and typed Razor You can used for Umbraco 7, even it says Umbraco 6.
Get random form entry
I have the requirement to show a random review on a client's website. I am saving the reviews fine using Umbraco Forms (V4.0.1) however I can't quite work out how to retrieve a random approved entry. The code I am using to retrieve the recordlist is as follows:
Can anyone tell me what the best way is to retrieve a random item from the list of items retrieved above?
As an aside, the reason I am doing an additional check on the record state is that GetApprovedRecordsFromForm is returning all records, even ones that are not approved.
Hi Graham,
Have you tried to use the .RandomOrder() to see if it works the way you like.
So it looks like this:
Hope this can help you.
/Dennis
Hi Dennis,
I will give that a try thanks. I presume that this just method just randomizes the items within the list and then you can just select the first item which should or should not be different when the method is next called?
Graham
Hi Graham,
Perhaps you need to do it in the foreach loop, but give it a try and see if it works. You can see the different methods here in the cheatsheets https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets. (PDF files). There are both cheatsheets for both dynamic Razor and typed Razor You can used for Umbraco 7, even it says Umbraco 6.
Hope this helps,
/Dennis
is working on a reply...