Mail Merge with Multiple data sources

4 posts, 0 answers
  1. Justin
    Justin avatar
    7 posts
    Member since:
    Jan 2011

    Posted 02 Sep 2011 Link to this post

    Is it possible to use a mail merge where the data can come from multiple data sources?

    For example, rather than having the data flattened out, due to the large number of tables from which to fetch the data and different types of summary records required, it would be simpler to be able to bind to multiple data sources.

    As a simplistic example, let's say we're displaying Customer information (FirstName, LastName, Address info which is still linked , etc) from one data source, and then order information from another (LastOrderAmount, DateLastOrdered, YTDOrderAmount, NumberOfOrdersPlaced, etc.). 

    Add to this another 10 different datasources such as the order information described above.

    To make this into a single data source would be quite a task, difficult to maintain, and possibly a performance hog.  Having multiple datasources would simplify and mitigate these concerns.

    Is this feasable?
  2. Boby
    Boby avatar
    763 posts

    Posted 07 Sep 2011 Link to this post

    Hello Attention: Norm,
    Actually mail merge fields support property path syntax; it's only the UI that has a limitation for adding merge fields with complex paths. You can, however, customize it to show buttons for adding such fields:
    1. Remove the binding of the button to the InsertMergeFieldEmptyCommand. In the default RadRichTextBoxRibbonUI, it is:
      <telerikRibbonBar:RadRibbonDropDownButton CollapseToMedium="WhenGroupIsMedium" LargeImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/32/InsertMergeField.png" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=InsertMergeFieldEmptyCommand}" Size="Large" Text="Insert Merge Field" />
      Instead, give the button a name like this, so that you can refer to that button in code behind:
      <telerikRibbonBar:RadRibbonDropDownButton CollapseToMedium="WhenGroupIsMedium" LargeImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/32/InsertMergeField.png" Size="Large" Text="Insert Merge Field" x:Name="insertMergeFieldButton" />
    2. Then, you can keep the column names which you wish to use as property paths for the merge fields in a property. Here, if needed, you can also use reflection to load the data source items properties.
      string [] ColumnNames = new string[]{"Name", "Age", "Address[0].Country", "Address[1].Country"};
      After that you can invoke the following method with a parameter this.insertMergeFieldButton:
      private void AddMergeFieldsInDropDownContent(RadRibbonDropDownButton radRibbonDropDownButton)
          StackPanel stackPanel = new StackPanel();
          foreach (string fieldName in this.ColumnNames)
              RadRibbonButton fieldButton = new RadRibbonButton()
                  Text = fieldName,
                  Size = ButtonSize.Medium,
                  HorizontalAlignment = HorizontalAlignment.Stretch,
                  HorizontalContentAlignment = HorizontalAlignment.Left
              fieldButton.Command = this.editor.Commands.InsertFieldCommand;
              fieldButton.CommandParameter = new MergeField() { PropertyPath = fieldName };
          stackPanel.Width = 140;
          radRibbonDropDownButton.DropDownContent = stackPanel;

    If the data items are unrelated, your can create 'mixed' collection and set it as mail merge data source, for example:

    this.radRichTextBox.Document.MailMergeDataSource.ItemsSource =
        from c in Clients
        from o in Orders
        select new
            FirstName = e.FirstName,
            Quantity = o.Quantity

    Don't hesitate to contact us if you have other questions.

    Best wishes,
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. Michael
    Michael avatar
    47 posts
    Member since:
    Oct 2010

    Posted 08 Dec 2013 Link to this post


    I want to share the following solution and request this feature to be added to the Telerik in the near future.

    I have a class which has many properties, but not all properties should be displayed in the Mail Merge.  I followed Boby's example to display only selected fields.  But instead of defining an array of strings for properties I created an Attribute class and defined it in my binded class itself.

    public class MailMergeAttribute : Attribute

     now, in my class I can define this Attribute for the properties I want to show up in the Mail Merge dropdown button. Then I have created a static property that return only properties that have MailMergeAttribute.

    public class Person
        public int PropInMailMerge1{ get; set; }
        public string PropInMailMerge2{ get; set; }
        public int PropNotInMailMerge{ get; set; }
        public string PropNotInMailMerge2{ get; set; }
        public static List<String> GetMailMergeFields()
            var props = from p in typeof(PatientNotification).GetProperties()
                            let attr = p.GetCustomAttributes(typeof(MailMergeAttribute), true)
                            where attr.Length == 1
                            select new { Property = p, Attribute = attr.First() as MailMergeAttribute };
            List<string> propsNames = new List<string>();
            foreach (var prop in props)
            return propsNames;

    Now, in Boby's example, replace this line

    foreach (string fieldName in this.ColumnNames)

    with this

    foreach (string fieldName in Person.GetMailMergeFields())

    And you have it.

    I would like to request from Telerik to define their own Attribute class and use this class to select which properties should show up in the Mail Merge dropdown.  In addition, maybe you can add DisplayName, Order and Group properties to this Attribute class.  DisplayName would allow developers to specify the name that should be displayed in the list. For example, property name could be FirstName, but in the list it is displayed as "First Name".  Order would define in which order fields should show up in the InsertMailMerge dropdown. Group would allow to group attributes into a sub-menu.  In case there are too many properties to be displayed in the list, some properties can be grouped under the sub-menu.

    Alternatively, this class can be created with a property "bool DoNotDisplay".  If developer did not specify this attribute, then the field is added as it is working now.  But if you define attribute with DoNotDisplay=true, then you do not show this property in the InsertMailMerge dropdown.  This way, developers don't have to add this attribute to all fields.



  4. Boby
    Boby avatar
    763 posts

    Posted 11 Dec 2013 Link to this post

    Hi Michael,
    Thank you for sharing the code here, as it may help others facing the same problem. Due to the existence of relatively easy way to customize the default UI, however, for now we are not planning to implement such behavior in the control.

    We have added points to your account for the suggestion. Don't hesitate to contact us if you have other questions.

    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top