October 14, 2016

How to get all the Lotus Notes Documents and its metadata from a Lotus Notes Database programmatically using C#


Before understanding the code for getting the Lotus Notes Documents, we will understand the structure of the Lotus Notes Document and how to analyse the properties of a Lotus Notes Document.

Understanding the Lotus Notes Database structure:

When we open any Lotus Notes Database in Lotus Notes Client, we can see different views of the documents in left panel. In right panel, we can see the respective document items of the view selected in left panel as shown in below image.


Now, to identify the Lotus Notes Documents metadata(properties), we need to double click document in the right panel.  It will show the Lotus Notes Document body with properties like Creator/Author, Created Date, Subject as shown in below image.


The Lotus Notes Document contains many other metadata apart from the above.  To get the other metadata, we need to open the document properties by right clicking the Lotus Notes Document item in the right panel as shown in below image.

In Document Properties window, we can analyse different properties for the documents as shown in below image.


Thus, we can analyse all the metadata of the Lotus Notes Document Item.

Programmatically accessing the Lotus Notes Documents and its properties:

We will understand the code step by step following the below sequence:

Note: This will require to add reference of "Interop.Domino.dll" assembly. This assembly is also available as NuGet Package.

Get Lotus Notes Database

Below code is used to get Lotus Notes Database instance from the Database Path and Domino Server:


        //Used to Get Notes Database from the Database Path and Domino Server.
        public static NotesDatabase GetDatabase()
        {
            InitializeSession();
            if (session == null)
            {
                return null;
            }
            return session.GetDatabase(Common.DominoServer, Common.DatabasePath, false);
        }

        private static NotesSession InitializeSession()
        {
            if (session != null)
                return session;
            session = new NotesSession();
            session.Initialize(Common.ClientPassword);
            return session;
        }

Note:
  1. Common. DominoServer is Domino Server full name.
  2. Common.DatabasePath is the database path in domino server.
  3. Common.ClientPassword is the password used for Lotus Notes Client.

Get all Documents Collection from database

Below code is used to get all the documents from the database:

     //Get document collection from Lotus Notes Database
     NotesDatabase database = null;
     NotesDocument doc = null;
     database = GetDatabase();
     NotesDocumentCollection documents = database.AllDocuments;
     doc = documents.GetFirstDocument();
     while (doc != null)
     {
         // Do Your Stuff with Lotus Notes documents
         doc = documents.GetNextDocument(doc);
     }

     //If we want to get documents from specific view, below code can be used:
     //Get “All” view from the Lotus Notes Database.
     NotesView view = null;
     NotesDatabase database = null;
     NotesDocument doc = null;
     database = GetDatabase();
     view = database.GetView("All");
     doc = view.GetFirstDocument();
     while (doc != null)
     {
         // Do Your Stuff with Lotus Notes documents
         doc = view.GetNextDocument(doc);
     }

Iterate the Lotus Notes Documents one by one and get its properties

Below code is used to access each documents properties from a document collection or a view:

     //Variables used to store Metadata
     static string category = string.Empty;
     static string subject = string.Empty;
     static string immediateParentSubject = string.Empty;
     static string newsLetterSubject = string.Empty;
     static string composedDate = string.Empty;
     static string composedTime = string.Empty;
     static object[] from = null;
     static object[] Authors = null;
     static string form = string.Empty;
     static object[] proLeader = null;
     static string body = string.Empty;
     static bool hasParent = false;
     
     //Iterating each document in a view or All Document collection.
     while (doc != null) {
      docMap = new Dictionary < NotesEmbeddedObject, string > (); //used to store the attachments of Notes Documents shown in body.
      object[] items = (object[]) doc.Items; //items object used to retrieve the properties of  document like Subject,  Body, etc.
      DateTime composedDateTime = (DateTime) doc.Created;

      //Retrieving  authors of Document
      Authors = (object[]) doc.Authors;
      if (Authors != null && Authors.Length > 0) {
       Authors = Authors.Where(val => val.ToString() != "").ToArray();
       Authors = Authors.Distinct().ToArray();
      }

      //If document has any attachments or any files
      if (doc.HasEmbedded) {
       NotesRichTextItem body = (NotesRichTextItem) doc.GetFirstItem("Body");
       if (body != null) {
        foreach(object filea in body.EmbeddedObjects) {
         NotesEmbeddedObject file = (NotesEmbeddedObject) filea;
         if (file.type.ToString() == "EMBED_ATTACHMENT") {
          docMap.Add(file, file.Source);
         }
        }
       }
      }

      //Get all properties of specific document from Lotus Notes database.
      foreach(NotesItem nItem in items) {
       if (nItem.Name == "Categories") // if item is “Categories” Property
       {
        category = ((object[]) nItem.Values)[0].ToString();
       } else if (nItem.Name == "Subject") // if item is “Subject” Property
       {
        subject = ((object[]) nItem.Values)[0].ToString();
       } else if (nItem.Name == "ParentForm") // if item is “ParentForm” Property
       {
        hasParent = true;
       } else if (nItem.Name == "ImmediateParentSubject") // if item is “ImmediateParentSubject” Property
       {
        immediateParentSubject = ((object[]) nItem.Values)[0].ToString();
       } else if (nItem.Name == "NewsLetterSubject") // if item is “NewsLetterSubject” Property
       {
        newsLetterSubject = ((object[]) nItem.Values)[0].ToString();
       } else if (nItem.Name == "Form") // if item is “Form” Property
       {
        if (((object[]) nItem.Values).Length > 0)
         form = ((object[]) nItem.Values)[0].ToString();
       } else if (nItem.Name == "From") // if item is “From” Property
       {
        if ((object[]) nItem.Values != null) {
         from = (object[]) nItem.Values;
        }
       } else if (nItem.Name == "ProLeader") // if item is “ProLeader” Property
       {
        if ((object[]) nItem.Values != null) {
         proLeader = (object[]) nItem.Values;
        }
       } else if (nItem.Name == "Body") // if item is “Body” Property
       {
        if (!string.IsNullOrEmpty(nItem.Values)) {
         body = body + nItem.Values;
        }
       }
      }
     
      doc = view.GetNextDocument(doc); // or doc=documents.GetNextDocument(doc);                                
     
     }

Thus, we can add conditions for more properties in above code snippet while iterating the document to access the property value.

To summarize, the above code helps us to access Lotus Notes Database, Lotus Notes Documents and Lotus Notes Document Properties programmatically using C#.Net.

If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment