Show and Tell Thursday: Dynamic Rich Text web content using WebQueryOpen...
Category Show And Tell Thursday
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...
Sub Initialize
'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.")
Else
Set docLookup = dcLookup.GetFirstDocument
Do While Not (docLookup Is Nothing)
Call rtiArticleData.AppendStyle(rtsArticleBold)
Call rtiArticleData.AppendText("Article: " + docLookup.altTitle(0) + _
" (" + Format$(docLookup.commonCreateModifyDate(0), "mm/dd/yyyy") + ")")
Call rtiArticleData.AddNewline(2)
Call rtiArticleData.AppendStyle(rtsArticlePlain)
Set varText = docLookup.GetFirstItem("altText")
Call rtiArticleData.AppendRTItem(varText)
Call rtiArticleData.AddNewline(2)
Call rtiArticleData.AppendStyle(rtsArticleBold)
Call rtiArticleData.AppendText("Conclusion")
Call rtiArticleData.AddNewline(2)
Call rtiArticleData.AppendStyle(rtsArticlePlain)
Set varConclusion = docLookup.GetFirstItem("altConclusion")
Call rtiArticleData.AppendRTItem(varConclusion)
Call rtiArticleData.AddNewline(3)
Set docLookup = dcLookup.GetNextDocument(docLookup)
Loop
End If
End Sub
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...
Sub Initialize
'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.")
Else
Set docLookup = dcLookup.GetFirstDocument
Do While Not (docLookup Is Nothing)
Call rtiArticleData.AppendStyle(rtsArticleBold)
Call rtiArticleData.AppendText("Article: " + docLookup.altTitle(0) + _
" (" + Format$(docLookup.commonCreateModifyDate(0), "mm/dd/yyyy") + ")")
Call rtiArticleData.AddNewline(2)
Call rtiArticleData.AppendStyle(rtsArticlePlain)
Set varText = docLookup.GetFirstItem("altText")
Call rtiArticleData.AppendRTItem(varText)
Call rtiArticleData.AddNewline(2)
Call rtiArticleData.AppendStyle(rtsArticleBold)
Call rtiArticleData.AppendText("Conclusion")
Call rtiArticleData.AddNewline(2)
Call rtiArticleData.AppendStyle(rtsArticlePlain)
Set varConclusion = docLookup.GetFirstItem("altConclusion")
Call rtiArticleData.AppendRTItem(varConclusion)
Call rtiArticleData.AddNewline(3)
Set docLookup = dcLookup.GetNextDocument(docLookup)
Loop
End If
End Sub



Comments
Posted by Christopher Byrne At 07:03:46 On 02/03/2006 | - Website - |
Posted by Bas At 08:09:23 On 02/03/2006 | - Website - |
I am curious why you could not do a DBLookup to preformatted text or used AJAX to make the call, styling it with CSS when loaded?
Posted by Christopher Byrne At 13:43:49 On 02/03/2006 | - Website - |
Posted by Duffbert At 14:50:49 On 02/03/2006 | - Website - |
Posted by Duffbert At 07:33:50 On 02/03/2006 | - Website - |
Posted by Ben Langhinrichs At 22:06:42 On 01/03/2006 | - Website - |