Friday, December 14, 2007

Drag & Drop From One Database To Another

I recently had someone ask me a question about some of the drag and drop techniques I use in my applications. One of the questions was whether or not you could drag and drop between databases. I can't recall ever having done that before, but with a little clever coding, it can be achieved. Here's how:

To accomplish this, I actually use the same technique that you may have seen in the presentation Nathan and I gave at Lotusphere last year.

The first step is to create a new folder that will serve as the target for the drag and drop action. The design of this folder doesn't really matter unless you want to use it as a folder in your source database. Once your folder is created, you want to add an entry for the folder on either an existing outline or create a new one. In the outline, reference the folder and add an icon if desired.

In order to get the documents being dragged from the source database to the target database, it is necessary to add code to the Queryaddtofolder event of the view/folder in which the document originally resides (not the one you are dragging it to). This code will perform the work of copying the source document to the destination database. Of course, there's lots of flexibility with how you could write this code (and it's probably best to throw it into a script library and use it in the views/folders you need it), but here's a sample I put into my mail file.

Sub Queryaddtofolder(Source As Notesuiview, Target As Variant, Continue As Variant)

'Here you could branch code based on the target folder.
'In my case, I don't want anything special to happen unless I'm dragging to the "DragTarget" folder.
If Target <> "DragTarget" Then Exit Sub

'In the case of my mail file, I don't want any docs actually going into this folder, so we'll set Continue to false, which will prevent this.
Continue = False

Dim session As New NotesSession
Dim collection As NotesDocumentCollection 'The documents selected (i.e. being dragged)
Dim doc As NotesDocument 'The doc in the source database
Dim newDoc As NotesDocument 'The doc in the target database

'Hardcoding is bad...don't do it! :-)
Dim targetDB As New NotesDatabase ( "", "mail1\gtd.nsf" )

'Get the collection of docs selected and iterate through...sending them to the target db and moving them to the appropriate folder
Set collection = Source.Documents
Set doc = collection.GetFirstDocument

Do While Not (doc Is Nothing)
Set newDoc= doc.CopyToDatabase( targetDB )
Call newDoc.PutInFolder("($Inbox)")
Set doc = collection.GetNextDocument(doc)

End Sub

This LotusScript was converted to HTML using the ls2html routine,
provided by Julian Robichaux at

This is a look at the implementation from my mail file. I have another database where I keep my to do lists. Often, an e-mail initiates some kind of action on my part, so rather than copying and pasting from my mail file to my Task database, I can just use the drag and drop feature. This is how it appears in Notes 8 integrated into the new look and feel. (Nothing exciting...looks like it's just part of the interface...which is what I wanted)

I made it look a little different in Notes 7 by using a bigger graphic to serve as the drop target. Again, this is just an outline embedded on a page, so you could use any kind of graphic you want.

Pretty handy and easy to implement as well. Enjoy!


Tim Tripcony said...

Shameless plug: this technique is how DesignCatalog (on OpenNTF) actually allows you to transfer design elements from one database to another...

Anonymous said...


Is it the same code used when you create a folder called "Archive" in your mail database when archiving rules are set ?

(then when you drag mails in this folder, they are moved to the archive database...)

Anonymous said...

How do you accomplish a "big" logo in the maildb outline ?
Is the icon within the outline or in the page that holds the outline ?