Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Graham Carr 277 posts 389 karma points
    Apr 17, 2015 @ 13:37
    Graham Carr
    0

    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:

    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.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Apr 17, 2015 @ 13:54
    Dennis Aaen
    0

    Hi Graham,

    Have you tried to use the .RandomOrder() to see if it works the way you like.

    So it looks like this:

    var recordlist = Library.GetApprovedRecordsFromForm("30769e6f-9de0-4365-b67f-5964876dc0f0").Items.Where(x => x.State.ToString() == "Approved").RandomOrder();

    Hope this can help you.

    /Dennis

  • Graham Carr 277 posts 389 karma points
    Apr 17, 2015 @ 13:57
    Graham Carr
    0

    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

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Apr 17, 2015 @ 14:09
    Dennis Aaen
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft