Skip to main content

Notifications

Community site session details

Community site session details

Session Id : egqprj9MK3jbLswMlC5DsQ

Get a random item from a SharePoint list

RobElliott Profile Picture Posted 16 Feb 2020 by RobElliott 10,134 Super User 2025 Season 1

There might be times when you want to get a random item from a SharePoint list. My list of Towns and Counties in the UK has about 1800 items in it and I want to get a random town once a day.

1. I've started with a Recurrence schedule as the trigger.

2. The first action is the SharePoint Get items.
0-recurrence-getItems.png

As my list has more than 100 items I have set Pagination on with a threshold of 2000 in the settings for Get items: 

0a-getItems-Settings.png

3. Next, add a Compose control and add the following expression:

body('Get_items')?['value'][rand(1,length(body('Get_items')?['value']))]

1-composeRand.png

4. At this point save the flow and test it. In the test results copy everything in the Output panel of the Compose:

2-run-copyComposeOutput.png

5. Next, edit the flow again and add a Parse JSON and from the dynamic content select the output from the Compose for the Contents field. Click Generate from sample and paste in the Schema code you copied at #4 above:

3-parseJSON.png

6. From the dynamic content you can now select the items from the Parse JSON to use in further actions in your flow. I'm just sending an email to myself but you could assign a task in planner, add the town to another list, use it to select a winner from a competition, where to go on your next vacation and for other uses where you want a random result from the SharePoint list:

4-sendEmail.png

The following is the result with a random town from the list:

5-emailResult.png

Rob

Los Gallardos

Categories:

Comments

  • jamescosten Profile Picture jamescosten 629
    Posted 30 Nov 2022 at 16:36:40
    Get a random item from a SharePoint list

    How do i return multiple randoms items, it is returning 1 itme but i would like 5.

  • magichappens89 Profile Picture magichappens89 2
    Posted 19 Nov 2021 at 13:36:22
    Get a random item from a SharePoint list

    I considered this but I only have 5 items and its pretty obvious its not picking the first and the last entry. I will try changing it to body('Get_items')?['value'][rand(0,length(body('Get_items')?['value']))] as I guess the first entry is 0 not 1. But that doesn´t explain why its not getting the last one.

  • RobElliott Profile Picture RobElliott 10,134 Super User 2025 Season 1
    Posted 19 Nov 2021 at 12:57:21
    Get a random item from a SharePoint list

    @magichappens89 yes I've had a couple of these running every day for about 3 years. Don't forget that being random it won't necessarily select an item, but then might pick it 3 times running....or not at all!

     

    1-PA.png

    Rob
    Los Gallardos
    If I've answered your question or solved your problem, please mark this question as answered. This helps others who have the same question find a solution quickly via the forum search. If you liked my response, please consider giving it a thumbs up. Thanks.

  • magichappens89 Profile Picture magichappens89 2
    Posted 19 Nov 2021 at 12:31:37
    Get a random item from a SharePoint list

    Anyone tried this out for a couple of runs? At least for me this never picks the items that are at the beginning or at the end so it´s not working very reliable.

  • Community Power Platform Member Profile Picture Community Power Pla...
    Posted 11 Nov 2021 at 11:01:24
    Get a random item from a SharePoint list

    Great tip, thank you very much!