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:

1 actions:
2    - kind: SetVariable
3      id: setVariable_AQUI9D
4      variable: Topic.table1
5      value: =[{Name:"option 1"},{Name:"option 2"}]
6
7    - kind: Foreach
8      id: Foreach_Nl2kasnd
9      items: =Topic.table1
10      value: Topic.item
11      actions:
12        - kind: SendActivity
13          id: sendActivity_ja5k6x
14          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.

Disclaimer

The content on Invoke, LLC's blog is provided for general informational purposes only and focuses on IT-related topics, including technology trends, software, AI, cybersecurity, and industry commentary. While we strive to provide accurate, up-to-date, and high-quality information, we make no representations or warranties of any kind, express or implied, about the accuracy, reliability, completeness, or suitability of the information, tools, or resources shared on this blog. Any reliance on such content is at your own risk.

The opinions expressed in our blog posts are those of the authors and do not necessarily reflect the official views of Invoke, LLC. Our blog may include links to third-party websites, software, or services. We do not endorse or assume responsibility for the content, functionality, security, or practices of these third-party resources.

The information on this blog is not intended to replace professional IT, technical, AI, or cybersecurity advice. You should consult a qualified IT professional before implementing any solutions, configurations, or strategies discussed on this blog. Invoke, LLC and its affiliates are not liable for any loss or damage, including but not limited to data loss, system downtime, or security breaches, arising from the use of or reliance on this blog’s content.

We reserve the right to modify, update, or remove content on this blog at any time without prior notice.

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