Manage Learn to apply best practices and optimize your operations.

Managing Lotus Notes doclinks with LotusScript

When working with numerous doclinks in a Notes/Domino application, it's easy to lose track of them. This LotusScript code from SearchDomino.com member Benoit Dubuc finds and validates all doclinks in a Lotus Notes database.

This LotusScript code finds all the doclinks in a Lotus Notes application and validates them. It also determines whether or not the target document is still in the Lotus Notes database.

To begin, use the code below to create a "Link Info" document for each doclink encountered. This needs to be in an agent that runs on all the documents in the Lotus Notes database:

Sub Initialize
'scan all fields to find rich text items 
and see if we have doclinks in them
 
Dim session As NotesSession
Dim db As NotesDatabase
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim rti As NotesRichTextItem
Dim rtnav As NotesRichTextNavigator
Dim rtlink As NotesRichTextDocLink
Dim LinksDB As NotesDatabase
Dim LinkDoc As NotesDocument

Set session = New NotesSession
Set LinksDB = session.CurrentDatabase
If Not LinksDB.isOpen Then 
Msgbox "Cannot locate Links DB.  Process ended."
Exit Sub
End If
Set db = session.CurrentDatabase
Set dc = db.UnprocessedDocuments
Set doc = dc.GetFirstDocument
 
While Not doc Is Nothing
'scan all items for Rich Tex t Fields
Forall i In doc.Items
If i.type =  1 Then
'scan the RTI for doc links (gets all links)   
Set rti = doc.GetFirstItem(i.name)
Set rtnav = rti.CreateNavigator
If rtnav.FindFirstElement
(RTELEM_TYPE_DOCLINK) Then
Do
Set rtlink = rtnav.GetElement
If rtlink.ServerHint = "" Then
 server$ = "Local"
Else
server$ = rtlink.ServerHint
End If
'get link info
Set LinkDoc = LinksDB.createdocument
linkDoc.form="LinkInfo"
linkDoc.Linkcomment = rtlink.DisplayComment
linkDoc.FoundInTitle = doc.title
linkDoc.FoundInUNID = doc.UniversalID
linkDoc.FoundForm = doc.form(0)
linkDoc.lcPhase = doc.lcPhase
linkDoc.cboProcess = doc.cboProcess
linkDoc.FoundIn = i.name
linkDoc.Server = server$
linkDoc.ReplicaID = rtlink.DbReplicaID
If  rtlink.ViewUNID <> String$(32, "0") Then
 
linkDoc.ViewUNID = rtlink.ViewUNID
End If
If  rtlink.DocUNID <> String$(32, "0") Then
linkDoc.DocUNID = rtlink.DocUNID
End If
Call linkdoc.save (True, False, True)
Loop While rtnav.FindNextElement
End If
End If
End Forall
Set doc = dc.GetNextDocument(doc)
 Wend
End Sub

Next, we need to use an agent that will try to open up those doclinks to see if the target doc is still in the Lotus Notes database. But first, you'll need to create a view that will show all "Link Info" documents, sorted by DocUNID:

Sub Initialize
'for all links, see if we can 
open the document/view/base

On Error Goto error_trap

Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim DB As NotesDatabase
Dim doc As NotesDocument
Dim view As NotesView
Dim TargetDB As New NotesDatabase( "", "" )
Dim TargetDoc As NotesDocument
Dim server As String
Dim i As Integer, count As Integer
Dim prevstatus As Double

Set db = session.CurrentDatabase
Set view = db.GetView( "LinksLookup" ) 
 'created to show the documents 
created by the previous agent
count = view.AllEntries.Count
Set view = db.GetView("(AllByDocID)")  
'needed to do a lookup
i=1
Set doc = view.GetFirstDocument
'---------------------------------------------------
------------------------------------------------------------   
'FOR NOW, ASSUME ALL LINKS 
ARE IN THIS DB
'If TargetDB.OpenByReplicaID
( server, doc.ReplicaID(0) ) Then
Set targetdb = db
'---------------------------------------------------
------------------------------------------------------------ 
While Not doc Is Nothing
If doc.hasitem("linkstatus") Then
prevstatus = doc.linkstatus(0)
Else
prevstatus = 999
End If
'see if DocUNID is not blank
If doc.docunid(0) <> "" Then
'can we get the Doc?
server = doc.server(0)
If Ucase(server) = "LOCAL" Then server = ""
'try to open the document
Set targetdoc = 
TargetDB.GetDocumentByUNID(doc.DocUNID(0) )
If targetDoc Is Nothing Then
doc.LinkStatus = 81  'not found
Else
doc.LinkStatus = 82  'found
End If
 Else
 'external link
 doc.linkstatus = 112
 End If
If doc.linkstatus(0) <> prevstatus 
Then Call doc.save(True, False, True) 
'so we don't generate useless replication
i=i+1
 
Set doc = view.GetNextDocument(doc)
Wend
Set targetdb = New NotesDatabase( "", "" )

Exit Sub

error_trap:
If Err = 4091 Then  'invalid UniversalID 
so we have an invalid doclink
Set targetdoc = Nothing
Resume Next
Else
Print "Error " + Cstr(Err) + 
" at line " + Cstr(Erl) + " : " + Error
Exit Sub
End If
 
 
End Sub

Last comes the tricky part of the effort. You may have images that have link hotspots on them that you want to validate. Here's a nifty way of doing it through DXL (Lotus Notes 6.5 or higher is required):

Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim exporter As NotesDXLExporter
Dim importer As NotesDXLImporter
Dim parser As NotesDOMParser
Dim node As NotesDOMDocumentNode
Dim nodelist As NotesDOMNodeList
Dim linknode As NotesDOMElementNode
Dim NodeParent As NotesDOMElementNode
Dim i As Long
Dim intCount As Integer
Dim DBUNID As String, ViewUNID As 
String, DocUNID As String, LinkText As String
Dim exportSucceeded As Boolean

Set db = session.CurrentDatabase
Set dc = db.UnprocessedDocuments
Set doc=dc.GetFirstDocument
Set exporter = session.CreateDXLExporter
Set parser = session.CreateDOMParser
 
Call exporter.SetInput(doc)
Call exporter.SetOutput(parser)
exporter.ExitOnFirstFatalError = False
exportSucceeded = False
Call exporter.Process
exportSucceeded = True
Set node = parser.Document
Set nodelist = 
node.GetElementsByTagName("doclink")
If nodelist.NumberOfEntries <> 0 Then
For i = 1 To nodelist.NumberOfEntries
Set linknode = nodelist.GetItem(i)
DBUNID = linknode.GetAttribute("database") 
ViewUNID = linknode.GetAttribute("view") 
DocUNID = linknode.GetAttribute("document") 
'get the text info for that link
Set NodeParent = linknode.parentnode
LinkText = nodeParent.GetAttribute("alttext") 
'make sure the link is still valid
  
  '...
'code to validate links can be 
used here, or create Link Info document... your call
 '...
Next
End If 
 
 
End Sub

One neat thing I added in this database is the option of having two embedded views: one showing doclinks in the current document, the other one showing links pointing to the current document. You should embed the view in the document's form and put the doc UNID as single category.

Do you have comments on this tip? Let us know.

Related information from SearchDomino.com:

  • FAQ: LotusScript advice
  • Learning Guide: LotusScript
  • Tip: Creating doclinks using undocumented LotusScript method
  • Reference Center: LotusScript tips and resources

    This tip was submitted to the SearchDomino.com tip library by member Benoit Dubuc. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our monthly tip contest and you could win a prize.

  • Dig Deeper on LotusScript

    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