This is a migrated thread and some comments may be shown as answers.

Mail Merge with Multiple data sources

3 Answers 187 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Justin
Top achievements
Rank 1
Justin asked on 02 Sep 2011, 06:18 PM
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?

3 Answers, 1 is accepted

Sort by
0
Boby
Telerik team
answered on 07 Sep 2011, 04:48 PM
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.Children.Add(fieldButton);
        }
       
        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,
Boby
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 >>

0
Michael
Top achievements
Rank 1
answered on 09 Dec 2013, 02:28 AM

Hi,


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
{
    [MailMergeAttribute]
    public int PropInMailMerge1{ get; set; }
 
    [MailMergeAttribute]
    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)
        {
            propsNames.Add(prop.Property.Name);
        }
 
        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.



Thanks,



Michael



0
Boby
Telerik team
answered on 11 Dec 2013, 05:09 PM
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.


Regards,
Boby
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
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 >>
Tags
RichTextBox
Asked by
Justin
Top achievements
Rank 1
Answers by
Boby
Telerik team
Michael
Top achievements
Rank 1
Share this question
or