Getting the first item from a SharePoint Online list in a Flow or Logic App without a For each Loop

Phew, what a long title!

Here’s the dealeo. You have a SharePoint list. You search it for an item from a Logic App or Flow. You know there’ll be a single match so you even add the ‘Top Count’ to be 1.

Yet when you try and say assign that output to a variable it creates a For each loop.

Notice I am selecting ‘value – Item’ to add to the Compose section here.

The moment I do that it makes this For each loop.

I don’t want that. I could work around it but why… I’d just like to get the first item from the SharePoint list.

If you Google there’s answers to this. I am just lazy to keep Googling each time so I’ll make a blog post so I know what to do next time.

The trick is to use the first function. All I have to do in the above case is change the Compose block to be something like this: first(body('Get_items')?['value'])?['<property I want>']

To the first function I pass the body function with the name of the step as its parameter. In the above case the step is called “Get items” so we write it as “Get_items” – replace spaces with underscores basically. So that becomes first(body('Get_items'). Notice I didn’t close the brackets, that’s intentional. What I want from body('Get_items') is the value, so I should have actually put body('Get_items')?['value'] – or with the first function first(body('Get_items')?['value']). This will return the first item in the collection of values returned from the “Get items” step.

I know from this item I want the “Name” column for instance. So I would write that as first(body('Get_items')?['value'])?['Name']. Notice the property is outside the first() block.

And that’s it!