Thursday, November 20, 2014

How to remove a document from all mail files by UNID

Ever have an email go out to everyone in the company that you really wanted to delete?  You can use message recall, but what if that doesn't work (you can turn it off) or the message came in from outside your network?

Below is the code to remove these documents.  Just fill in the server name and the mail file folder name in the application.  Set the agent properties to run from the actions menu and set the target to none.  The biggest thing to remember is that you must have manger-like access to the mail files in order to delete the document

%REM
   Agent Delete Across System by UNID
   Created Nov 20, 2014 by David
   Description: Given a UNID, this agent will search for and delete a document that has that ID
%END REM
Option Public
Option Declare

Sub Initialize
   '<Initialization>
   Dim ses As New NotesSession
   Dim dbdir As New NotesDbDirectory("")
   Dim db As NotesDatabase
   Dim thisdb As NotesDatabase 
   Dim doc As NotesDocument
   Dim mailfolder As String
   Set thisdb = ses.currentdatabase

   Dim servers(1 To 1) As String 'What server(s) to run on
   servers(1)="MailServer/Name"

   Dim doc_id(1 To 1) As String
   doc_id(1) = "F000E656739BFECD1A1D2D04FA1E56FD"

   '<Set value to the variables mailfolder>
   mailfolder = "mail"

   ForAll v In servers

      Set dbdir = ses.GetDbDirectory(v)
      Set db = dbdir.GetFirstDatabase(DATABASE)

      Print Left(db.Filepath,Len(mailfolder))

      '<Prints out current database>
      While Not(db Is Nothing)
        If Left(db.Filepath,Len(mailfolder)) = mailfolder Then
          db.Open "",""
          '<Check if the database is in the current specified mail folder>
          If InStr(db.filepath, mailfolder+"\") > 0 Then
          'This is code for deleting all id at one shot
            ForAll w In doc_id
              On Error 4091 Resume Next 'Trap for not finding the UNID
              Set doc = db.GetDocumentByUNId(w)
              If (Not doc Is Nothing) Then
                Print "Found and removed the document in " + db.title+" on "+ db.server
                Call doc.remove(True)
              End If
            End ForAll
          End If
        '<Next database.........>
        End If
Set db = dbdir.GetNextDatabase
    Wend
   End ForAll
   Print "Completed removing the document from mail files in " + mailfolder
End Sub

I hope this helps you out as much as it has me.  If you find any bugs or anything please let me know so I can fix my code.  

No comments: