AI Business Solutions

Undocumented: Foreach in Copilot Studio

Zewei Song, Ph.D.
May 13, 2025

I will cut the chase: there is a way to do for-each loop in Copilot Studio and it is hidden, meaning you cannot find it in its designer UI.

This short article will show you how to use it.

Follow below steps:

1. In Copilot Studio, go to your Custom Copilot and open an existing topic or create a new one.

2. Switch to the topic's code editor by click on Moreon the designer UI's top nav bar and chose "Open code editor".

3. Once you are you are in the code editor, add following lines to the end of the topic's source code:

1kind: Foreach
2      id: Foreach_Nl2kasnd

4. The alphanumeric string after Foreach_ is just a random identifier so anything would work.

5. Now Close the code editor and you should see a newaction called "Loop through a list" appearing with errors.

6. Don't worry about the errors, we will fix them soon: we need a table variable for the foreach loop and some logic inside the loop.

7. Add an action of "Set a variable value" right before the foreach block and create a new table variable named table1 and set its value to [{Name:"option 1"},{Name:"option 2"}]

8. Go to the "Loop through a list" action we have just created and set "Items to loop over" to table1 and set the "Loop value variable" to "item". It should look like this after the setting:

9. Now we can finally add logics in the loop. let's add a "Send a message" action to the loop and display each record in the table. To do that just make the message to show "item name is: {Topic.item.Name}".

The entire logic should look like this now:

If you test this topic, your topic should return something like below:

The code-behind for all added actions:

 actions:
    - kind: SetVariable
      id: setVariable_AQUI9D
      variable: Topic.table1
      value: =[{Name:"option 1"},{Name:"option 2"}]

    - kind: Foreach
      id: Foreach_Nl2kasnd
      items: =Topic.table1
      value: Topic.item
      actions:
        - kind: SendActivity
          id: sendActivity_ja5k6x
          activity: "item name is: {Topic.item.Name}"

Conclusion

Foreach is not yet published but it is fully implemented, well almost: only thing missing is the designer UI component to add it to the design view, but you can add it via the code editor and it is a must for processing table variables.

Invoke combines tightly coupled security controls and digital productivity solutions.

WANT TO LEARN MORE?

Invoke’s system integration solutions maximizes your Microsoft investments through exclusive programs, expert access, cost optimization, proven methodologies, and industry expertise.

PROFESSIONAL SERVICES

Microsoft
Entra
Microsoft
Defender XDR
Microsoft
Purview
Microsoft
Intune
Maximize Your Microsoft Investments 
Optimize Costs and Efficiency
Unified Security
Operations
Let’s talk about how we can help bring you the power and digital innovation of Microsoft technologies to your business!
Thank you! We have received your information and will contact you shortly!
Oops! Something went wrong while submitting your information. Please try again in a few minutes!
© Invoke, LLC 2025