How to: Insert Dynamic Content
- 3 minutes to read
The code sample below shows how use the DOCVARIABLE field and RichEditControl.CalculateDocumentVariable event to insert dynamic (formatted) content. In this example, the DOCVARIABLE field is used to display the DevExpress community website’s RSS feed.
Note
A complete sample project is available at Dynamic Content demo module.
- Load a document template, if required.
Insert a DOCVARIABLE field. Use the FieldCollection.Create method, as shown below:
Use the RichEditDocumentServer object to generate or load the target dynamic content. In this example, the GenerateRssFeed method returns this RichEditDocumentServer object.
private static RichEditDocumentServer GenerateRssFeed() { RichEditDocumentServer rssProcessor = new RichEditDocumentServer(); Document document = rssProcessor.Document; //Create a new bulleted list AbstractNumberingList abstractNumberingList = document.AbstractNumberingLists.BulletedListTemplate.CreateNew(); document.NumberingLists.CreateNew(abstractNumberingList.Index); SyndicationFeed feed = null; //Extract feed items try { using (XmlReader reader = XmlReader.Create("https://community.devexpress.com/blogs/MainFeed.aspx")) { feed = SyndicationFeed.Load(reader); } } catch { return null; } //Format each feed item into the bullet list item document.BeginUpdate(); foreach (SyndicationItem item in feed.Items) //This method inserts each item as a hyperlink with publication date AddSyndicationItem(document, item); document.EndUpdate(); return rssProcessor; }
Handle the RichEditControl.CalculateDocumentVariable event. Use the RichEditDocumentServer object created in the previous step as the CalculateDocumentVariableEventArgs.Value property value. Identify the field by the CalculateDocumentVariableEventArgs.VariableName property value.
Note
The current DOCVARIABLE field value may be lost if you update this field in another word processing application, where the CalculateDocumentVariable event isn’t used. Use one of the following approaches to suppress DOCVARIABLE field updates:
- Set the CalculateDocumentVariableEventArgs.FieldLocked property to true in the CalculateDocumentVariable event handler.
- Replace document fields with their values.
void richEditControl_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e) { //Retrieve the field by its ID if (e.VariableName == "rssFeed") { //Retain the last paragraph of the inserted document in the resulting document e.KeepLastParagraph = true; //Use the method result as the field's value e.Value = GenerateRssFeed(); if (e.Value != null) //Keep the current field value e.Handled = true; //Suppress the field update e.FieldLocked = true; } }
Call the FieldCollection.Update to update all document fields.
public DynamicContentModule() { //Load a document template richEditControl.LoadDocument("Dynamic Content.docx"); //Insert the DOCVARIABLE field InsertDocVariableField(richEditControl.Document); //Subscribe to the CalculateDocumentVariable event richEditControl.CalculateDocumentVariable += richEditControl_CalculateDocumentVariable; //Update all document fields richEditControl.Document.Fields.Update(); }