• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 646
  • Last Modified:

How to delete documents from view

I'm not a Lotus Notes expert, so forgive any misuse of terminology
I'm hoping this isnt too complicated, but I've oodles of question points available to spend on it if it does...

I have a domino database that receives messages daily from lots of different sources, and an agent that identified the ones Im looking for, and puts them into a view.
I want to scan the view, keep the latest document from each source (its a unique document subject), and delete all older copies.

I've written some code (Perl) that uses the Notes.NotesSession object to:
Open the view
Create a viewnavigator
and loop through each document via ViewEntry ->GetNextDocument, so I can extract all the details I need.

I intend to compare (for each document subject) the posted date in order to determine the one I need to keep, and mark the 'other' one for deletion.
This could be either from either the current viewentry I have, or the one I've stored previously.

I've got it happily printing out what its doing, what records its looking at, and identifying the correct ones to delete, but haven't got it yet actually doing it

I dont have any problems with the coding, but I dont understand the Domino COM implementation well enough to know if:

1. is this an acceptable way to do it?
2. Is there a way to order the documents in the viewnavigator so I get the newly posted ones first?
3. If I need to delete a previously 'stored' document reference - what would I need to store in order to retrieve it, and do the deletions affect the viewnavigator.

any advice or code snippets appreciated - I dont mind what language they come in!

hope this makes sense....
  • 4
  • 2
  • 2
2 Solutions
Sjef BosmanGroupware ConsultantCommented:
Re your questions:
1) Id' say no, it is not; why don't you use Domino the way it is supposed to be used? You can program that function in a database agent, and it'll be activated automatically by the server. No need for external applications whatsoever.
2) Yes, you have to reorder the view itself: for example, the first column could contain the function @Created; then, set it to decreasing order
3) I think I don't understand... what if your first column were the subject, and the second column the creation date?

> any advice or code snippets appreciated - I dont mind what language they come in!
I might have some advice for you, shall I continue in Dutch or French...?    ;-))
It's acceptable, why not (unless you can access Domino database design, then I agree with Sjef).

If you don't have access to domino db design, then you'll have to find max date/time the doc is modified, that's no problem...

Why don't you post the code you have so far?
BobMcAuthor Commented:
Thanks for the responses. Yes, I understand this would be better / easier done within Domino, but I don't have that luxury in this case.

I've posted the interesting bit of the code I have so far.
I'm keeping track of the newest document by subject in a hash, so it doesnt matter what order I search the documents. I either need to delete the current document, or the one I already referenced in the hash.
I was sort of hoping that I could save a unique ID for each document, and delete it with that, but Im unsure how to proceed

I'll all ears if theres a better / different approach to doing this via COM that I should consider.

$NotesSession = Win32::OLE->new('Notes.NotesSession')
                or die "Cannot start Lotus Notes Session object.\n";
$NotesDB = $NotesSession->GetDatabase($servername, $databasename)
               or die ("Can't open database $databasename\n");
               or die ("Can't open view $viewname\n");
my $doccount=$NotesViewNav->{Count};
print "There are $doccount documents in view $viewname to process\n";
my $docindex=0;     ## Current doc position in ViewNav - dont know if this is useful
my %LatestDocs=();  ## Capture the details of the latest posted
                    ## document for each subject
while (defined ($NotesViewEntry)) {
  $NotesDocument = $NotesViewEntry->Document;
  ## extract all the fields at once
  my $doccollection = $NotesDocument->{Items};
  ## convert to perl hash ref
  my $docitems=&CollectionToHash($doccollection);
  ## We assume that the subject will never change, and is unique per server
  ## So we need to check that within the view, this is the latest (or only)
  ## document (subject) for this server.
  ## If the current subject is not in the hash, so its the first we've found for
  ## this server, and we store it
  ## If the current subject is already in the hash, and the current is older,
  ## we delete it
  ## If the current subject is already in the hash, and the current is newer,
  ## we delete the stored one, and store the current one
  ## Need to ensure we extract the next document before we delete anything.
  ## Subject is "title string : server name". Dont need title bit
  my ($subject) = ($docitems-> {Subject} =~ /:(.*)/);
  my $posteddate= $docitems->{PostedDate};
  print "Doc[ $docindex / $doccount ]:  Subject $subject\t\tPosted at $posteddate\n";
  ## Convert date to numeric for comparison (tz excepting)
  my $eposteddate=DateStringToEpoch($posteddate);
  ## Do we already have one of these?
  if (exists($LatestDocs{$subject})) {
    ## Already got one - is it newer that the current one?
    if ($LatestDocs{$subject} >= $eposteddate) {
      ## Stored doc is not older -> keep stored one and delete current
      print "Delete current doc for $subject, posted at $posteddate\n";
    } else {
      ## current doc is newer
      ## so delete stored doc, and save current details
      print "Replace stored doc for $subject posted at "
      print "With current doc for $subject posted at $posteddate\n";
  } else {
    ## new subject => this one is newest for now, so store it
    print "New - store current doc for $subject \n";
  last unless $docindex < $debugdoccount;

Open in new window

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Sjef BosmanGroupware ConsultantCommented:
Every document has a unique key, and an almost unique key (only unique per replica of a database). Use doc.UniversalID.
Sjef BosmanGroupware ConsultantCommented:
And to move back to a document, use view.GetDocumentByUnid()
GetDocumentByUnid is member of NotesDatabase, not NotesView (GetDocumentByKey).

So, to get the document use:
Set curDoc = db.getDocumentByUNID( "earlierSavedUnid" )

To delete the doc use:
Call curDoc.RemovePermanently

To remove doc only from the view (and leave it in a db) you'll have to figure out which flag is used to show documents in that view (analyze either Agent that puts docs in a view or View selection formula). Then use:
Call curDoc.RemoveItem( "itemName" )

to remove flag field.
BobMcAuthor Commented:
thanks for the help guys, I've managed to trash several replicas now, so I know I'm on the right track :)
Sjef BosmanGroupware ConsultantCommented:

Very good!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now