MailMerge/Application Field Question

12 posts, 2 answers
  1. Cameron Molyneux
    Cameron Molyneux avatar
    79 posts
    Member since:
    Jan 2010

    Posted 05 Jan 2012 Link to this post

    I'm using the mailmerge fields to create a structured report

    i.e. the field is a 'question' that can be answered ar runtime, i.e a combo box or something else

    I have a my questions defined in a DB

    I use the propertypath property to display the 'Question' to the user, I'd like another property on a mergefield that I can store other data .. like a tag property, that is saved with the document etc

    Is this possible??

    We used to use TXTextControl and we use APPLICATION FIELDS rather than MERGEFIELDS as this lets us create and save a param list

    Is there something similar avaliable?
  2. Answer
    Boby
    Admin
    Boby avatar
    595 posts

    Posted 10 Jan 2012 Link to this post

    Hi Cameron,
    You can use custom field inheriting from merge field and add some properties (as demonstrated in the attached project). You should verify if they are correctly preserved because our XAML serializer may not successfully serialize them depending of the custom property type:
    public class CustomMergeField : MergeField
    {
        private const string CustomFieldName = "CustomFieldName";
     
        static CustomMergeField()
        {
            CodeBasedFieldFactory.RegisterFieldType(CustomMergeField.CustomFieldName, () => { return new CustomMergeField(); });
        }
     
        [XamlSerializable]
        public string Tag { get; set; }
     
        public override string FieldTypeName
        {
            get
            {
                return CustomMergeField.CustomFieldName;
            }
        }
     
        public override void CopyPropertiesFrom(Field fromField)
        {
            CustomMergeField mergeField = fromField as CustomMergeField;
            if (mergeField != null)
            {
                this.Tag = mergeField.Tag;
            }
     
            base.CopyPropertiesFrom(fromField);
        }
     
        public override Field CreateInstance()
        {
            return new CustomMergeField();
        }
    }


    Regards,
    Boby
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Cameron Molyneux
    Cameron Molyneux avatar
    79 posts
    Member since:
    Jan 2010

    Posted 10 Jan 2012 Link to this post

    Brilliant!

    Thanks I'll give that a try now

    :)
  5. Cameron Molyneux
    Cameron Molyneux avatar
    79 posts
    Member since:
    Jan 2010

    Posted 12 Jan 2012 Link to this post

    Hi

    We have tried this here with three text properties (string) and when we add the to the document it seems to work as we have some temp code that checks the fields are in the document correctly


    We then save the document as DOCX

    When we load it all of the text properties we created are null

    Any ideas?

    Thanks
  6. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 16 Jan 2012 Link to this post

    Hi Cameron,

    Whereas the "normal" merge fields are persisted in docx, HTML and XAML, custom merge fields and custom annotation ranges can be persisted only in XAML. The solution Boby suggested shows how the document model can be extended to provide similar functionality, but the export and import will work only with the XAML serialization.
    Once master/detail mail merge is genuinely implemented, we will adjust the format providers to persist the merge fields correctly in the case of master/detail mail merge with the other formats as well. The feature, however, is not scheduled for 2012 Q1, so it will not be implemented before Q2.

    Kind regards,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  7. Mike
    Mike avatar
    4 posts
    Member since:
    Aug 2012

    Posted 16 Feb 2012 Link to this post

    Hi there,

    I am trying to achieve this, I have created some properties in my custom MergeField class, and saved the document in XAML format to a byte array, with Unicode encoding on the load and save.

    Please see code below based on your example for the custom properties. I'm not sure what I'm missing here.

    When I use the format provider to import the XAML string, I get an error "The type 'SynopticMergeField' was not found."

    Thanks in advance!

    public class SynopticMergeField : MergeField
       {
           private const string QuestionTextstr = "QuestionText";
     
           static SynopticMergeField()
           {
               CodeBasedFieldFactory.RegisterFieldType(SynopticMergeField.QuestionTextstr, () => { return new SynopticMergeField(); });
           }
     
           [XamlSerializable]
           public string QuestionText { get; set; }
           [XamlSerializable]
           public string OriginalField { get; set; }
           [XamlSerializable]
           public string ID { get; set; }
           [XamlSerializable]
           public string FieldName { get; set; }
     
           public override Field CreateInstance()
           {
               return new SynopticMergeField();
           }
     
           public override string FieldTypeName
           {
               get
               {
                   return SynopticMergeField.QuestionTextstr;
               }
           }
     
           public override void CopyPropertiesFrom(Field fromField)
           {
               SynopticMergeField mergeField = fromField as SynopticMergeField;
               if (mergeField != null)
               {
                   this.QuestionText = mergeField.QuestionText;
                   this.OriginalField = mergeField.OriginalField;
                   this.ID = mergeField.ID;
                   this.FieldName = mergeField.FieldName;
               }
               base.CopyPropertiesFrom(fromField);
           }
       }
  8. Ivailo Karamanolev
    Admin
    Ivailo Karamanolev avatar
    276 posts

    Posted 20 Feb 2012 Link to this post

    Hi,

    The most probable reason for that error is that static constructors aren't executed until a member/property of that class is accessed. Therefore, it may happen so that the field is not registered when importing the document. Please make sure that it is registered before doing the import. If that proves not the be the issue, please contact us again to investigate further.

    Regards,
    Ivailo Karamanolev
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  9. Mike
    Mike avatar
    4 posts
    Member since:
    Aug 2012

    Posted 20 Feb 2012 Link to this post

    Hi thanks for your reply,

    I realised it was a case of having the wrong namespace in the XAML generated from the document, by default it was coming back like this:

    <t:RadDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:t="clr-namespace:Telerik.Windows.Documents.Model;assembly=Telerik.Windows.Documents" xmlns:s="clr-namespace:Telerik.Windows.Documents.Model.Styles;assembly=Telerik.Windows.Documents"
    And the tags created for my custom MergeField were as follows:

    <t:SynopticMergeField ... />
    And of course my class does not exist in that namespace, so I had to add the namespace definition in programmatically before the Export, and replace all <t:SynopticMergeField with my own definition name.

    I have it working now with this workaround.

    Thanks, 
    Mike.
  10. Cameron Molyneux
    Cameron Molyneux avatar
    79 posts
    Member since:
    Jan 2010

    Posted 23 Feb 2012 Link to this post

    Hi,

    I'm working with Mike to get this working,

    Our current progress is we are getting all the data back for the mailmerge and are at the point now where we want the mailmerge to take place.
    We are not setting a MailMergeDataSource, and want to manually mailmerge, replacing the mergefield with text but also keeping the mergefield in place.

    We have the text we want to display, but we're not sure of how to go about setting it, without calling .MailMerge(false) or setting a datasource.

    I did try setting the datasource to a list on my viewmodel, which contains columns like "Full Name", and the mergefield's property path is set to "Full Name" however when I right click the mergefield and update, it disappears from the document.

    Thanks.
  11. Answer
    Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 23 Feb 2012 Link to this post

    Hello Mike, Cameron,

    Let me address your questions separately.
    @Mike:
    With the 2011.3.1220 version of the controls, custom mail merge fields are exported in the following way:

    <t:FieldRangeStart AnnotationID="2">
      <t:CustomMergeField DisplayMode="Code" PropertyPath="Orders" xmlns:t="clr-namespace:SilverlightApplication1;assembly=SilvelrightApplication1" />
    </t:FieldRangeStart>

    provided that SilverlightApplication1 is the name of your application and the namespace of the field. Redefining the namespace on the custom merge field resolves the issues with the namespace, so you don't need to do any additional processing of the exported document.

    @Cameron:
    In order to be able to set the result of the evaluation of a merge field, you can override the GetResultFragment() of MergeField. In this way, you can perform the mail merge without getting the values for the result from the MailMergeSource, but in another way. Please find attached a demo illustrating how this can be done.
    When it comes to your other concern, when you perform a mail merge, the merge fields are not removed from the document. Their display mode changes, but they are still present in the document. You can verify that by exporting and importing a document from the demo.

    The only thing you should be wary when using such custom merge fields is that the UI for inserting a merge field will not work unless you set an items source to the MailMergeDataSource. 

    I hope this helps.

    Kind regards,
    Iva Toteva
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  12. Cameron Molyneux
    Cameron Molyneux avatar
    79 posts
    Member since:
    Jan 2010

    Posted 24 Feb 2012 Link to this post

    Hi Iva,

    Thanks very much for your reply, this is working great!

    One last question regarding our custom MergeField, is there any way to make them readonly after they've been dragged on as we do not want the users to be able to edit or remove them in the document.

    We need them to be readonly only after our template has been loaded, not when the templates are being designed.

    Thanks.
  13. Ivailo Karamanolev
    Admin
    Ivailo Karamanolev avatar
    276 posts

    Posted 29 Feb 2012 Link to this post

    Hi,

    Fields don't have an option to become read-only by themselves, however, RadDocument offers some options to make elements read-only:
    • Document protection: using this feature, you'll be able to insert ranges, where the user will be able to edit the document when the protection is enabled. When disabled, the user will be able to edit the entire document.
    • Read-only ranges: this is the inverse, where you specify ranges which will be read-only for the user. This will probably be better suited for the purpose, as you can insert one read-only range around each field you insert, thus rendering it read-only.
    One other thing you should have in mind is that the document protection ranges can be persisted in docx, RTF and XAML, while ReadOnlyRanges are only imported/exported in XAML. Let us know if you need additional assistance.
    Greetings,
    Ivailo Karamanolev
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top
DevCraft banner