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.
As my list has more than 100 items I have set Pagination on with a threshold of 2000 in the settings for Get items:
3. Next, add a Compose control and add the following expression:
body('Get_items')?['value'][rand(1,length(body('Get_items')?['value']))]
4. At this point save the flow and test it. In the test results copy everything in the Output panel of the Compose:
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:
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:
The following is the result with a random town from the list:
Rob
Los Gallardos
Great tip, thank you very much!
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.
@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!
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.
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.
How do i return multiple randoms items, it is returning 1 itme but i would like 5.