How to delete documents from view

Posted on 2009-04-30
Last Modified: 2013-12-18
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....
Question by:BobMc
    LVL 46

    Expert Comment

    by:Sjef Bosman
    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...?    ;-))
    LVL 22

    Expert Comment

    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?
    LVL 7

    Author Comment

    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

    LVL 46

    Accepted Solution

    Every document has a unique key, and an almost unique key (only unique per replica of a database). Use doc.UniversalID.
    LVL 46

    Expert Comment

    by:Sjef Bosman
    And to move back to a document, use view.GetDocumentByUnid()
    LVL 22

    Assisted Solution

    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.
    LVL 7

    Author Comment

    thanks for the help guys, I've managed to trash several replicas now, so I know I'm on the right track :)
    LVL 46

    Expert Comment

    by:Sjef Bosman

    Very good!!

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    For Desktop Techs: How to retain a user's Notes configuration data when swapping out the end user's computer. (Assuming that you are not upgrading to a completely different version of Notes client) All you need to do is: 1) install Notes o…
    Notes Document Link used by IBM Notes is a link file which aids in the sharing of links to documents in email and webpages. The posts describe the importance and steps to create a Lotus Notes NDL file in brief.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now