web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Foreach loop not working in CodeBlocks

(0) ShareShare
ReportReport
Posted on by 115

We have a requirement where we need to list all Preference Lists that the contact receiving the email has been subscribed to, so we can dynamically show or hide different content blocks related to those preferences within the email.

As suggested here https://docs.microsoft.com/en-us/dynamics365/marketing/dynamic-email-content and here https://community.dynamics.com/365/marketing/b/dynamics-365-for-marketing-community-blogs/posts/add-dynamic-content-to-marketing-emails-and-show-event-sessions, the only way to do that is to insert a Code Block element in the marketing email and provide with these code lines:

{{#each contact.listmember_list_contact}}
{{#if (eq this.listid '"   blockListId   "')}}"
contentBlockHTML
"{{/if}}
{{/each}}

However this is not working at all. Even if we simplified to this: 

    {{#each contact.listmember_list_contact}}
  • ID: {{this.listid}}
  • {{/each}}

Nothing appears, only blank space. 

What are we doing wrong?

Best regards

I have the same question (0)
  • Verified answer
    Nya Profile Picture
    29,060 on at

    Hi, Manuel

    Does the Preference Lists you mentioned refer to the Marketing Lists?

    If so, it’s an N: N relationship between Marketing List and Contact while the example you referred to is an N: 1.

     8546.png

    Unfortunately, N: N relationship has not been supported in Code Block.

    For this type of relationship is not linked by the specified field, it is not possible to find the corresponding list by emailing the identified contact.

     

    You can find the description about N: N relationship in the documentation you mentioned:

    Add dynamic content to marketing emails (Dynamics 365 Marketing) | Microsoft Docs

     6710.png

    And here is a related issue:

    many to many relation - Dynamics 365 Marketing Forum Community Forum

     

    If this helped you, I'd appreciate it if you'd mark this as a Verified Answer, which may in turn help others as well.

    Best Regards,

    Nya

  • Manuesev Profile Picture
    115 on at

    Thanks for answer

    We are using the N:N relationship between Contact and Marketing List: 

    pastedimage1613724245066v1.png

  • James DC - Marketing Profile Picture
    10 on at

    Hi there,

    So I am trying to solve this exact issue currently; building an email that displays content blocks based off subscription/marketing lists.

    For example, say a contact is in subscription lists 'Cars' and 'Boats', but not 'Bikes' - I'd like to display the content block for 'Cars' and the contact block for 'Boats'.

    What I have so far is using:

    {{#if (eq contact.listmember_list_contact Cars)}}

    ...not sure how to link in the contact block for Cars...

    {{else if (eq contact.listmember_list_contact Boats)}}

    ...not sure how to link in the contact block for Boats...

    {{else}}

    ...fallback content block

    {{/if}}

    Any help on this would be amazing.

    Thanks in advance!

  • Dsouza_28 Profile Picture
    7 on at

    Hello James DC - Marketing ,

    I am currently exploring dynamics email content in the similar lines as you mentioned. Where you able to figure out a solution?

    I'd appreciate your response.

  • James DC - Marketing Profile Picture
    10 on at

    Hi Roy - unfortunately nothing at this point. My team is beginning to look into this further so hopefully an update/solution is on the way.

    J

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 50 Super User 2026 Season 1

#2
Eugen Podkorytov Profile Picture

Eugen Podkorytov 49

#3
Fiona_Tiernan Profile Picture

Fiona_Tiernan 22 Moderator

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans