|
| 1 | +--- |
| 2 | +title: Performing Nested MailMerge with Multiple Levels in RadWordsProcessing |
| 3 | +description: Learn how to implement nested MailMerge operations with multiple levels of data, such as handling lists within lists, in RadWordsProcessing. |
| 4 | +type: how-to |
| 5 | +page_title: How to Handle Nested MailMerge with Multi-Level Data in RadWordsProcessing |
| 6 | +slug: nested-mailmerge-radwordsprocessing |
| 7 | +tags: radwordsprocessing, mailmerge, nested, data, list, document processing |
| 8 | +res_type: kb |
| 9 | +ticketid: 1668943 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 2024.3.806| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +When attempting to perform a MailMerge operation with multiple levels of nested data, such as a list within a list (e.g., `Incident` > `Person` > `Phones`), an issue occurs where the nested collection enumerator exhausts prematurely. How can one resolve multi-layer nesting for MailMerge operations in RadWordsProcessing? |
| 21 | + |
| 22 | +This KB article also answers the following questions: |
| 23 | + |
| 24 | +- How to perform a nested MailMerge operation in RadWordsProcessing? |
| 25 | +- How to use MailMerge with lists within lists in RadWordsProcessing? |
| 26 | +- How to avoid premature exhaustion of the nested collection enumerator during MailMerge in RadWordsProcessing? |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +To achieve a nested MailMerge operation with multiple levels of data, follow the steps below: |
| 31 | + |
| 32 | +1. Prepare your data model to reflect the nested structure. In this case, the model includes `Incident`, `Person`, and `Phone` classes. |
| 33 | + |
| 34 | +2. Use the `MailMerge` method to merge the data with the document template. Ensure your document template has the appropriate merge fields defined for each level of data. |
| 35 | + |
| 36 | +3. Use special merge fields (`TableStart`, `TableEnd`, `RangeStart`, and `RangeEnd`) to denote the beginning and end of each nested collection. |
| 37 | + |
| 38 | +Here is an example demonstrating how to set up your data model and perform the nested MailMerge: |
| 39 | + |
| 40 | +```csharp |
| 41 | +// Define your data models |
| 42 | +public class Incident |
| 43 | +{ |
| 44 | + public string ReportNumber { get; set; } |
| 45 | + public List<Person> People { get; set; } |
| 46 | +} |
| 47 | + |
| 48 | +public class Person |
| 49 | +{ |
| 50 | + public string FirstName { get; set; } |
| 51 | + public string LastName { get; set; } |
| 52 | + public List<Phone> Phones { get; set; } |
| 53 | +} |
| 54 | + |
| 55 | +public class Phone |
| 56 | +{ |
| 57 | + public string PhoneNumber { get; set; } |
| 58 | +} |
| 59 | + |
| 60 | +// Preparing the data |
| 61 | +var mergeData = new List<Incident>{ |
| 62 | + new Incident{ |
| 63 | + ReportNumber = "INC-2024-001", |
| 64 | + People = new List<Person>{ |
| 65 | + new Person{ |
| 66 | + FirstName = "John", |
| 67 | + LastName = "Doe", |
| 68 | + Phones = new List<Phone>{ |
| 69 | + new Phone{ PhoneNumber = "310-555-0101" }, |
| 70 | + new Phone{ PhoneNumber = "310-555-0102" } |
| 71 | + } |
| 72 | + }, |
| 73 | + // Add more Person instances as needed |
| 74 | + } |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +// Perform the MailMerge operation |
| 79 | +RadFlowDocument document = new RadFlowDocument(); |
| 80 | +// Assume 'provider' is initialized and points to the appropriate document format provider |
| 81 | +var mailMergeResult = document.MailMerge(mergeData); |
| 82 | +``` |
| 83 | + |
| 84 | +In your document template, ensure you have the corresponding merge fields: |
| 85 | + |
| 86 | +- For the start and end of the `People` list: `TableStart:People` and `TableEnd:People`. |
| 87 | +- For the start and end of the `Phones` list within each `Person`: `RangeStart:Phones` and `RangeEnd:Phones`. |
| 88 | +- For merging individual property values, use merge fields named after the properties, such as `FirstName`, `LastName`, and `PhoneNumber`. |
| 89 | + |
| 90 | +### Generating the Necessary Table Structure in the Document |
| 91 | + |
| 92 | +When dealing with nested collections, it's crucial to dynamically create table structures that can accommodate the varying lengths of these collections. Below is an example showing how to generate a table in the document for `People` and their `Phones`: |
| 93 | + |
| 94 | +```csharp |
| 95 | +private static void GenerateTable(RadFlowDocumentEditor editor) |
| 96 | +{ |
| 97 | + // Example code to generate a table structure for People and Phones |
| 98 | + // This code should be adapted to fit the structure of your document template |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +By following these steps and utilizing the provided code snippets, you can effectively perform nested MailMerge operations with multiple levels of data in RadWordsProcessing. |
| 103 | + |
| 104 | +## See Also |
| 105 | + |
| 106 | +- [Nested MailMerge Documentation](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/editing/mail-merge#nested-mail-merge) |
| 107 | +- [Generating a Word Document with Data Using MailMerge in RadWordsProcessing](https://docs.telerik.com/devtools/document-processing/knowledge-base/generate-doc-template-and-populate-with-collection-data-mail-merge) |
| 108 | +- [RadFlowDocument Overview](https://docs.telerik.com/document-processing/libraries/radwordsprocessing/model/radflowdocument) |
0 commit comments