“I’d like to select a random image from my OneDrive every day and post it on Twitter, can I do it with Power Automate?”
When you build flows in Power Automate, you probably base them on a specific input. A specific Excel file that contains Planner tasks, specific SharePoint item with an approval process, or an email with specific attachment. But sometimes you don’t need a specific row/file/item. You might want a random input instead, e.g. a random image from a folder, a random row from an Excel file, or a random news. For example, to build a flow that’ll post a random funny picture on your Twitter every day.
Get all the available inputs
Before you can select a random input, you must first know what the possible inputs are. For this example it’ll be all the images in the OneDrive folder /Images.
Get the position of the random input
The next step is to calculate how many images you’ve got. Use the length(…) expression to get the number of available images.
length(outputs('List_files_in_folder')?['body/value'])
This number will give you the top limit. If you imagine the inputs as a list of files, the length is the last possible position for a file, while 0 is the first possible position. Use the rand(…) expression to get a random position between 0 and that number.
rand(0,length(outputs('List_files_in_folder')?['body/value']))
The random number can be then used as a position of the random image.
Get the input itself
At this moment you’ve get everything you need. You’ve got the list of all the possible inputs, and you have a randomly selected position of one of them. Since the inputs are a JSON array, you can use the position as the index.
outputs('List_files_in_folder')?['body/value'][<index>]
or
outputs('List_files_in_folder')?['body/value'][rand(0,length(outputs('List_files_in_folder')?['body/value']))]
All that’s left is to take the required value from the input, in this case the image id to get the file content. Once you have the content you can post it on Twitter.
outputs('List_files_in_folder')?['body/value'][<index>]?[<property>]
or
outputs('List_files_in_folder')?['body/value'][rand(0,length(outputs('List_files_in_folder')?['body/value']))]?['Id']
Summary
There’re three main points in this article. Firstly, you can use Power Automate even to select a random value for you, you don’t have to pick a specific entry every time. In this example it was a random image, but you can randomly select and process anything. Secondly, it’s another example of combining expressions. You could use multiple ‘Compose’ actions to build each part of the expression, or you can do everything at once. And thirdly, Power Automate is great in the Microsoft 365 platform, but it’s not limited to it. If you start using connections to other tools/apps you can do so much more.
Hi. Your solution How to select a random image from OneDrive with Power Automate helped me a lot. Could you also give me a tip on how to move the image after a tweet post to a new OneDrive folder
you need to use an expression that will select a file from the list of files in folder in order. Here’s how you can do it:
Instead of the triggerBody() expression?[‘$foreach’][‘item’]?[‘Index’], use the intDiv() function to divide the current loop variable value by 1:
intDiv(triggerBody()?[‘$foreach’][‘item’], 1)
This will allow you to get the current file index in the list of files in the folder
Use this expression instead of the previous one to select the file in order:
outputs(‘List_files_in_folder’)?[‘body/value’][intDiv(triggerBody()?[‘$foreach’][‘item’], 1)][‘Id’]
This expression will select a file from the list of files in the folder in order, using the current index from the loop variable.
Hello Andry,
thank you for sharing the solution.