Recreating print view on the Web

To recreate the "Print View" functionality of the Notes client on the web, I used a single agent.

The client wanted to recreate the "Print View" functionality of the Notes client on the web. I use a single agent to handle printing all or selected documents. A query string value ("type=") passed in the URL triggering the agent determines whether selected or all documents will be set up for printing. If the user wants to print selected documents, we need to invoke some JavaScript (primarily the printDocs() function) to build a string of the UNIDs of all selected documents. This string is then embedded as part of the query string of the URL triggering the agent.

Printing all documents is relatively straightforward: I use the GetFirstDocument and GetNextDocument methods of the NotesView class combined with a Do Loop to cycle through all documents in the view.

Printing selected documents is a little trickier. An <INPUT> tag (column formula) in conjunction with the addToPrintList() JavaScript function enables users to select the documents to be printed. The function builds a string (printString) of the UNIDS for all documents that the user checks ( e.g., "&id0=unid1&id1=unid2&id2=unid2..."). The printDocs() function then embeds this string as part of the query string in the URL triggering the PrintDocs. A count of documents selected (count) is also passed along in query string (you may have noticed that the count is also used to distinguish UNIDs in printString). This count is used to set the upper threshold of a For Loop in which each document's ColumnValues are used to build a table row of data. Within the For Loop, the getQryStrValue function is used to extract a specific document UNID from the query string.

Whether printing all or selected documents, the agent builds and returns an HTML table containing the view column data of the documents. It uses the NotesViewColumn Title property to build a row of column titles (getColumnTitles function) and the NotesDocument ColumnValues property to populate subsequent rows of the table (getColumnValues function).

The code below is based on a scenario in which a user can print a view that has been embedded on a page.

Although the code here is set to print a single view, it would not take many revisions to make it possible for the user to select the view they wanted to print.


Formula of first column of view to be printed:
"[<input type=checkbox value=" + @Text(@DocumentUniqueID) + " onClick=addToPrintList(this.value)>]"

"Print All Documents" Action on page:
var path = location.pathname.slice( 1, location.pathname.lastIndexOf("/"));
window.location.href = "/" + path + "/PrintDocs?openagent&type=All"

"Print Selected Documents" Action on page:
if (printString == "") {
	alert("No documents have been selected.");
}
else if (count >= 61) {
	//according to a Lotus Technote, Domino's URL parser (R5) can handle at least 64 ampersands within a URL
	alert("You cannot select more than 60 documents at a time.");
	document.forms[0].reset();
} else {
	printDocs();
}

JS Header of page containing embedded view:
//these variables are initialized to "" and 0, respectively, in the onLoad event of the page containing the embedded view
var printString;
var count;

function printDocs() {
	//called from "Print Selected Documents" action
	var qrystr = printString + "&count=" + count + "&type=Selected";
	var path = location.pathname.slice( 1, location.pathname.lastIndexOf("/"));
	window.location.href = "/" + path + "/PrintDocs?OpenAgent" + qrystr;
}

function addToPrintList(unid) {
	//called from view column formula
	printString += "&id" + count + "=" + unid;
	count += 1;
}

PrintDocs agent:
Option Public

Dim db As notesdatabase
Dim view As notesview
Dim doc As notesdocument
Dim printdoc As notesdocument

Dim returnTable As String

Sub Initialize
'this agent build and returns an HTML table that mimics the instantiated view object
'this enables the user to "recreate" the "PrintView" functionality found on the Notes client
'a Print button is also returned to facilitate printing from the browser
	
	Dim s As New notessession
	
	Dim printType As String
	Dim tableTitle As String
	
	On Error Goto errHandler
	
	Set db = s.currentdatabase
	Set view = db.getview("Trading Partner for Web")
	Set doc = s.documentcontext
	
	printType = getQryStrValue(doc.Query_String(0), "type")
	tableTitle = "Title of Table"
	
	'starts HTML table, adding view column titles as first row of table
	returnTable = "<h2>" + tableTitle + " (" + printType + ")</h2><br>"
	returnTable = returnTable + "<table bgcolor=FFFFFF border=0 width=100%>"
	returnTable = returnTable + "<tr>" + getColumnTitles + "</tr>"
	
	If printType = "Selected" Then
		Call printSelected
	Elseif printType = "All" Then
		Call printAll
	Else
		Goto errHandler
	End If
	
	'ends HTML table, adding a "Print" button at the end
	returnTable = returnTable + "</table><br><br>"
	returnTable = returnTable + "<center><input type=button value=Print onClick=javascript:window.print()></center>"
	
	Print returnTable
	
	End
	
errHandler:
	Print "<body bgcolor=E0E0E0><form><center><font color=993333>"
	Print "<br><h3>The following error has occurred:"
	Print "<br>" + Error + " (" + Cstr(Err) + ")"
	Print "<br><br>Please contact support.</h3>"
	Print "</font><center></form></body>"
	End
End Sub

Function getColumnTitles As String
'called from Initialize
'this function loops through all of the view's columns, grabbing each column title along the way
'each column title is inserted into a cell of the HTML table (the <TR> and </TR> tags are set in Initialize)
'the resulting string is returned to Initialize, where it is added to the HTML table
	
	Dim nvc As notesviewcolumn
	Dim temp As String
	
	For y = 1 To Ubound(view.columns) 'y is set to 1 because the first column of the view is an <INPUT> tag
		Set nvc = view.columns(y)
		
		temp = temp + "<td><b>" + nvc.title + "</b></td>"
		'temp = temp + "<th colspan=1 align=left>" + nvc.title + "</th>"
	Next
	
	getColumnTitles = temp
	
End Function

Function getColumnValues As String
'called from Initialize
'this function loops through all documents in the view
'each document starts a new row in the HTML table
'for each document, each value in the document's ColumnValues array is inserted into a cell of the HTML table
'the resulting string is returned to Initialize, where it is added to the HTML table
	
	Dim temp As String
	
	colArray = printdoc.columnvalues
	
	temp = temp + "<tr>"
	
	For x = 1 To Ubound(colArray) 'x is set to 1 because the first column of the view is an <INPUT> tag
		
		If colArray(x) = "" Then
			colArray(x) = " "
		End If
		
		temp = temp + "<td>" + colArray(x) + "</td>"
	Next
	
	temp = temp + "</tr>"
	
	getColumnValues = temp
	
End Function

Function getQryStrValue(Byval qryStr As String, Byval itemToFind As String) As String
'this function returns the value of the query_string (passed in as "qryStr") item passed in as "itemToFind"
	
	Dim baseValue As String	
	Dim itemPosition As Integer
	Dim multiVals As Integer
	Dim valPosition As Integer
	
	itemPosition = Instr(1, qryStr, itemToFind, 4)
	
	If itemPosition > 0 Then
		baseValue = Mid$(qryStr, itemPosition, Len(qryStr)) 'lops off query_string to left of itemToFind
		multiVals = Instr(1, baseValue, "&", 4) 'tests for additional items
		
		If multiVals > 0 Then
			baseValue = Mid$(baseValue, 1, multiVals -1) 'lops off everything to the left of the first ampersand
		End If
		
		valPosition = Instr(1, baseValue, "=", 4) 'finds equals sign that separates item from value
		
		If valPosition > 0 Then
			baseValue = Mid$(baseValue, valPosition + 1, Len(baseValue))
			getQryStrValue = baseValue
		Else
			Goto Failure
		End If
		
	Else
Failure:
		getQryStrValue = ""
	End If
	
End Function

Sub printAll
	
	Dim noDocsStr As String
	
	Set printdoc = view.getfirstdocument
	
	If printdoc Is Nothing Then
		'if the view contains no documents, inform user
		noDocsStr = "There are no documents in the " + view.name + " view"
		Print "<br><br><center><font color=000000><h2>" + noDocsStr + "</h2></font><center>"
		End
	Else
		
		Do Until printdoc Is Nothing
			'for each document in the view, build an HTML table row from its ColumnValues property
			returnTable = returnTable + getColumnValues
			
			Set printdoc = view.getnextdocument(printdoc)
		Loop
		
	End If
	
End Sub

Sub printSelected
	
	Dim qsdoc As notesdocument
	
	Dim unid As String
	Dim count As Integer
	
	count = Cint(getQryStrValue(doc.Query_String(0), "count")) - 1
	
	'uses variable pulled from query_string to set the number loop iterations (i.e. how many documents have to be processed)
	For x = 0 To count
		'selected doc UNID is pulled from query_string based on current iteration number and used to instantiate a document
		unid = getQryStrValue(doc.Query_String(0), "id" + Cstr(x))
		Set qsdoc = db.getdocumentbyunid(unid)
		
		'instantiate the current document from the view (in order to access ColumnValues property)	
		'make sure that the first column of the view, that generates <INPUT> tags, is not sorted
		Set printdoc = view.getdocumentbykey(qsdoc.FirstSortedColumnFieldName(0), True)
		
		'for each document in the view, build an HTML table row from its ColumnValues property
		returnTable = returnTable + getColumnValues
	Next
	
End Sub

Dig Deeper on Domino Resources

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close