Show and Tell Thursday: Dynamic Rich Text web content using WebQueryOpen...
Having not done a lot of web development in Domino until the last couple of years, I have to 'fess up... I've never used WebQueryOpen/WebQuerySave agents, nor have I ever had to figure out the DocumentContext property. That all changed today...
I have an application that gathers information on companies, and one of the features displays a printable web page with all the information about said company. At the bottom of the page, I needed to have a section that displayed content for 0 to many articles related to the specified company. Normally I'd just think computed text or embedded view, but the article information is rich text. And instead of just linking them to the article, we need to display the text of the piece so that it will be printable along with the other company information. I was afraid I had coded myself into a corner on this one until I started to play with WebQueryOpen.
Basically, I added a computed Rich Text field on the web form and then added a WebQueryOpen agent to build the value for that field before the document opens. I wasn't sure that I could do that because of all the quirks of Rich Text, but it works like a charm. Since the DocumentContext gives access to the document *before* it gets rendered to HTML, I guess the rich text that you add to the document isn't constrained by all those "front-end class" limitations you see in the Notes client. Regardless, I was *thrilled* when I added the agent, refreshed the form, and got the dynamic rich content from a lookup view showing however many articles that are related to the company... Not rocket science, but it made me feel good... :)
Here's the agent I used in the WebQueryOpen event...
'Build a dynamic Rich Text field for the webform based on contents from other documents.
Dim session As New NotesSession
Dim dbThis As NotesDatabase
Dim docThis As NotesDocument
Dim viewLookup As NotesView
Dim dcLookup As NotesDocumentCollection
Dim docLookup As NotesDocument
Dim rtiArticleData As NotesRichTextItem
Dim rtsArticleBold As NotesRichTextStyle
Dim rtsArticlePlain As NotesRichTextStyle
Dim varText As Variant
Dim varConclusion As Variant
Set dbThis = session.CurrentDatabase
Set docThis = session.DocumentContext
Set viewLookup = dbThis.GetView("vuwLookupArticles")
'This is the rich text field we're building, along with some styling...
Set rtiArticleData = New NotesRichTextItem(docThis, "altArticleData")
Set rtsArticlePlain = session.CreateRichTextStyle()
rtsArticlePlain.Bold = False
rtsArticlePlain.NotesColor = COLOR_BLACK
Set rtsArticleBold = session.CreateRichTextStyle()
rtsArticleBold.Bold = True
rtsArticleBold.NotesColor = COLOR_BLACK
'Get the articles for this company from the lookup view
Set dcLookup = viewLookup.GetAllDocumentsByKey(docThis.compCompany(0), True)
'Either build the content or say there weren't any Articles for that timeframe.
If dcLookup.Count = 0 Then
Call rtiArticleData.AppendText("No articles within the last 90 days.")
Set docLookup = dcLookup.GetFirstDocument
Do While Not (docLookup Is Nothing)
Call rtiArticleData.AppendText("Article: " + docLookup.altTitle(0) + _
" (" + Format$(docLookup.commonCreateModifyDate(0), "mm/dd/yyyy") + ")")
Set varText = docLookup.GetFirstItem("altText")
Set varConclusion = docLookup.GetFirstItem("altConclusion")
Set docLookup = dcLookup.GetNextDocument(docLookup)