Exporting Documents from Teamroom/NSF files using C# Revisited

9/21/2010

A while back I posted some code that would go through a directory and export documents from every NSF file (Lotus Notes Database file) it came in contact with. Thankfully I really didn't have to do much else with our Lotus install, until now that is. I was recently given the task of exporting everything from our Lotus install and and put them into SharePoint. Not that hard really, I found an app that would automatically upload documents into a SharePoint list with no issues and I had my code from before so I thought, no problem.

I grabbed the nsf files that I needed, started exporting, and suddenly I was hit with OutOfMemory exceptions... Turns out a couple of those nsf files were about a gig each and my dev box has about 2 gigs of memory and my previous implementation wasn't that great on memory usage... So that leads me to version two of the code:

   1: public partial class Form1 : Form
   2:  
   3: {
   4:  
   5:     public Form1()
   6:  
   7:     {
   8:  
   9:         InitializeComponent();
  10:  
  11:     }
  12:  
  13:  
  14:  
  15:     public static void GetDocuments(string StartDirectory, string OutputDirecotry)
  16:  
  17:     {
  18:  
  19:         List<System.IO.FileInfo> Files = Utilities.FileManager.FileList(StartDirectory);
  20:  
  21:         List<System.IO.DirectoryInfo> Directories = Utilities.FileManager.DirectoryList(StartDirectory);
  22:  
  23:         foreach (System.IO.FileInfo File in Files)
  24:  
  25:         {
  26:  
  27:             if (File.Extension.Equals(".nsf", StringComparison.CurrentCultureIgnoreCase))
  28:  
  29:             {
  30:  
  31:                 if (File.Name.Equals("Main.nsf"))
  32:  
  33:                 {
  34:  
  35:                     GetFromNSFdb(File.FullName, OutputDirecotry);
  36:  
  37:                 }
  38:  
  39:                 else
  40:  
  41:                 {
  42:  
  43:                     Utilities.FileManager.CreateDirectory(OutputDirecotry + "/" + File.Name.Replace(".nsf",""));
  44:  
  45:                     GetFromNSFdb(File.FullName, OutputDirecotry);
  46:  
  47:                 }
  48:  
  49:             }
  50:  
  51:         }
  52:  
  53:         foreach (System.IO.DirectoryInfo Directory in Directories)
  54:  
  55:         {
  56:  
  57:             if (!Directory.Name.Equals("Search.ft", StringComparison.CurrentCultureIgnoreCase))
  58:  
  59:             {
  60:  
  61:                 Utilities.FileManager.CreateDirectory(OutputDirecotry + "/" + Directory.Name);
  62:  
  63:                 GetDocuments(Directory.FullName, OutputDirecotry + "/" + Directory.Name);
  64:  
  65:             }
  66:  
  67:         }
  68:  
  69:     }
  70:  
  71:  
  72:  
  73:     public static void GetFromNSFdb(string strNSFdbName,string OutputDirectory)
  74:  
  75:     {
  76:  
  77:  
  78:  
  79:         try
  80:  
  81:         {
  82:  
  83:             Domino.ISession session = new NotesSessionClass();
  84:  
  85:             session.Initialize("");
  86:  
  87:             Domino.NotesDatabase database = session.GetDatabase("", strNSFdbName, false);
  88:  
  89:             Domino.NotesNoteCollection noteCollecton = database.CreateNoteCollection(true);
  90:  
  91:             noteCollecton.SelectDocuments = true;
  92:  
  93:             noteCollecton.BuildCollection();
  94:  
  95:             string NextDoc = noteCollecton.GetFirstNoteId();
  96:  
  97:             while (!string.IsNullOrEmpty(NextDoc))
  98:  
  99:             {
 100:  
 101:                 Domino.NotesDocumentClass TempDoc = (Domino.NotesDocumentClass)database.GetDocumentByID(NextDoc);
 102:  
 103:                 if (TempDoc.HasEmbedded)
 104:  
 105:                 {
 106:  
 107:                     object[] items = (object[])TempDoc.Items;
 108:  
 109:                     foreach (NotesItem item in items)
 110:  
 111:                     {
 112:  
 113:                         if (item.Name.Equals("$FILE"))
 114:  
 115:                         {
 116:  
 117:                             object[] values = (object[])item.Values;
 118:  
 119:                             TempDoc.GetAttachment(values[0].ToString()).ExtractFile(OutputDirectory + "/" + values[0].ToString());
 120:  
 121:                         }
 122:  
 123:                     }
 124:  
 125:                 }
 126:  
 127:                 NextDoc = noteCollecton.GetNextNoteId(NextDoc);
 128:  
 129:             }
 130:  
 131:         }
 132:  
 133:         catch {  }
 134:  
 135:     }
 136:  
 137:  
 138:  
 139:     private void button1_Click(object sender, EventArgs e)
 140:  
 141:     {
 142:  
 143:         GetDocuments(textBox1.Text, textBox2.Text);
 144:  
 145:         MessageBox.Show("Done");
 146:  
 147:     }
 148:  
 149:  
 150:  
 151:     private void textBox1_Click(object sender, EventArgs e)
 152:  
 153:     {
 154:  
 155:         folderBrowserDialog1.ShowNewFolderButton = false;
 156:  
 157:         if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
 158:  
 159:         {
 160:  
 161:             textBox1.Text = folderBrowserDialog1.SelectedPath;
 162:  
 163:         }
 164:  
 165:     }
 166:  
 167:  
 168:  
 169:     private void textBox2_Click(object sender, EventArgs e)
 170:  
 171:     {
 172:  
 173:         folderBrowserDialog1.ShowNewFolderButton = true;
 174:  
 175:         if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
 176:  
 177:         {
 178:  
 179:             textBox2.Text = folderBrowserDialog1.SelectedPath;
 180:  
 181:         }
 182:  
 183:     }
 184:  
 185: }

If you look at the original code, I was doing some funky things. However at the time there was almost no documentation that I could find on working with Lotus in .Net. Since then it seems like it's much easier to find and thus I made a number of improvements. The code is much smaller, much faster, and should be a bit easier to follow. And thankfully it takes very little memory to run and thus I was able to export the documents from the nsf files that were a gig with no issues. So anyway, take a look, leave feedback, and happy coding.



Comments