SharePoint list number of items
IntroductionIm my previous post, I explained how to use the SharePoint Get items action in Flow. As the name implies, it retrieves items from a SharePoint list. Show Sometimes you need to know if your Get items action returned any items. For example, if you wanted to update an existing item or create a new item in none was found. In this post, Ill show you how to count how many items were returned by SharePoint and how to test if any items were found. And dont worry, this post wont be as long as the last one. Counting results from SharePoint Get itemsFor the purpose of this example, well assume that you already created a flow with a SharePoint Get items action. If you havent done so yet, take a look at my previous post. When I have to use fancy formulas in many places within my flow, I like to define a variable. That way, I can just refer to the variable instead of re-entering the formula in many places.
When using variables in flow, you use a different action to define a variable the first time (Initialize variable) than you would to set the variable or change its value (Set variable, Increment variable, and Decrement variable for example). Since this is the first time we set the variable, well use Initalize variable using the following steps:
Save and test your flow. Mine returned 1 item: Testing if any items were returnedNow that you have a variable that contains the number of items, you can use it anywhere you want. For example, if you wanted your flow to do something if any items were returned, and something else if nothing was returned you would follow these steps:
Save and test your flow. If everything worked well, the Expression value from your condition should return true if SharePoint found items, and false if nothing was found. Of course, you would want to add actions to your If yes and If no paths, but thats for another post. ConclusionThe key to testing if the SharePoint Get items action returned items it to understand that Get items returns a collection of items. Using the length() function against the return value of your Get items action will tell you the length of your collection of items. I could have avoided using a variable and just entered the length(body('Get_items')?['value']) formula directly in the condition, but I wouldnt be able to tell how many items were returned when I was testing the flow. This sample was an easy one, and I really didnt need to evaluate how many items were returned more than once so I really didnt need a variable but in more complicated flows, youll find it a lot easier to define variables and use the variables throughout instead of copying the same formula every time. I hope this helps? |