Access calendar data Lotus Notes C#

人盡茶涼 提交于 2021-01-29 10:50:12

问题


I'm trying to access a Lotus Domino database. Can it be done with C#? Are there some examples out there that I could look at?


回答1:


There are lots of examples out there, just try searching for it. This example could get you started:

//This DLL you've got to add under Project-> Add Reference --> COM Tab --> Lotus Domino Objects
//Standard Path for this DLL is: "C:\Program Files\Notes\domobj.tlb"
using Domino;   //domobj.tlb*
...

try
{
    //-------------------------------------------
    //!!Important!!
    //Before you start, you have to check 2 things
    //1.) Lotus Notes has to run when you start this application
    //2.)The files "notes.ini" and "user.id" 
    // has to be in the main Lotus Notes folder
    //--------------------------------------------    

    //First, create a new Lotus Notes Session Object
    Domino.NotesSession LNSession = new Domino.NotesSession();
    //Next add a Database and a Document Object (not new)
    Domino.NotesDatabase LNDatabase;
    Domino.NotesDocument LNDocument;
    //Initialize your Session with your Password
    LNSession.Initialize("password");

    //Connect to your Notes Server and the path of your 
    //.nsf File (in my case its in a subfolder 'mail').
    LNDatabase = LNSession.GetDatabase("Notes-Server", "mail\\user.nsf", false);
    //Create an in memory document in the server database
    LNDocument = LNDatabase.CreateDocument();
    //-------Assign Field Values-------
    //Define Start&End Date+Time of your appointment
    //Year, Month, Day, Hour, Minute and Second
    System.DateTime StartDate = new DateTime(2008, 3, 19, 8, 2, 0);
    System.DateTime EndDate = new DateTime(2008, 3, 19, 8, 5, 0);
    //This Defines that it is an Calendar Entry
    LNDocument.ReplaceItemValue("Form", "Appointment");
    //Type of the appointment, means:
    LNDocument.ReplaceItemValue("AppointmentType", "0");
    //0 = Date, Appointment           
    //1 = Anniversary
    //2 = All Day Event (Do Not Set Time Here!)
    //3 = Meeting
    //4 = Reminder
    //5 = Date (Special, experimental!)    
    // Title of your entry
    LNDocument.ReplaceItemValue("Subject", "hello world");

    // Set Confidential Level (Public=1 or Private=0) 
    LNDocument.ReplaceItemValue("$PublicAccess","1");    

    //Add Start&End Time of your event
    LNDocument.ReplaceItemValue("CALENDARDATETIME", StartDate);
    LNDocument.ReplaceItemValue("StartDateTime", StartDate);
    LNDocument.ReplaceItemValue("EndDateTime", EndDate);
    LNDocument.ReplaceItemValue("StartDate", StartDate);
    LNDocument.ReplaceItemValue("MeetingType", "1");
    //Infos in The Body
    LNDocument.ReplaceItemValue("Body", "Body Text Body Text ...");
    //Add an alarm to your appointment
    LNDocument.ReplaceItemValue("$Alarm", 1);
    LNDocument.ReplaceItemValue("$AlarmDescription", "hello world (alarm)");
    LNDocument.ReplaceItemValue("$AlarmMemoOptions", "" );
    //5 = Time (in minutes) before alarm goes on
    LNDocument.ReplaceItemValue("$AlarmOffset", 5);
    LNDocument.ReplaceItemValue("$AlarmSound", "tada");
    LNDocument.ReplaceItemValue("$AlarmUnit", "M");
    //This saves your Document in the Notes Calendar
    LNDocument.ComputeWithForm(true, false);
    LNDocument.Save(true, false, false);
    //On success, you'll see an info message;
    MessageBox.Show("Calendar Entry Successfully Added!", "Info", 
        MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception e1)
{
    //On error you'll see an error message
    MessageBox.Show(e1.Message);
}
...



回答2:


First of all my condolences for the choice of mail server your company has forced upon you.

The problem is that there really are not and good examples out there. There is only a small community of users that still use this dead technology.

Here's what I found:

As screwed up as this sounds.. Open the nsf file in a hex editor - there are some "good" examples written in VB (or Lotus Script) in there. o_O

But as far as I can tell (at least for myself) the calendar information is not stored in the local nsf files. The problems we are facing are:

  1. What is the server name / database name? I can find about 10 different domino server ip addresses if I look around for them - can I use just any of these? Does knowing the SMTP address help?
  2. What string to I pass to GetView? I've seen "Contacts" "People" "$(People)" putting something stupid in there returns null, is there a list somewhere??
  3. Where does the user come from? In the rest of the universe Authentication uses a user name and password. Why doesn't the Notes API want to know the username??

To answer 1: I think a blank string somehow grabs the correct server from the registry god knows where.

Another possibility is to try this.. On the Lotus Notes there is a down arrow which expands to allow you to navigate to Mail, Calendar, (and a bunch of other crap that you've probably never used). Right click Calendar here then go to Application > Properties. It will list the server and filename. Remove the trailing /Garbage/Crap from the server name. That will give the server name for the function below. Also remove the prefixing \mail\directory from the filename that will give the database for the function below.

To answer 2: I've found no better answer than using a Hex editor or scouring the internet for code snippets. I suspect that this string is set not by IBM but during the installation. So if we could find the default value that *might work. Again I'd like a better answer myself. What works for me is $All for E-Mail (sorry In lotus notes there are no "emails" Lotus sends "Memos") and Calendar for the calendar.

To answer 3: Lotus Notes API seems to use the last logged on user for the authentication of the next logging on user. Go figure this is probably why Lotus is so more secure btw.

The following function requires a reference to the Domino dll. You might need to reference it, and if your on a 64 bit system it still won't work. Compile this in 32 bit mode. Don't forget Lotus Notes is a "Enterprise level application" and light years beyond anything Outlook can do. This will return a series of Key/Value pairs. Yes you read that right. It's all key value pairs. A lot of 64 bit encoded garbage, color codes, guids, seemingly random arrays of "1", and other useless crap fortunately the data you want is in clear text.

using System;
using System.Linq;
using System.Data;
using System.Collections;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using Domino;
using System.Text;
using System.IO;
using System.Collections.Generic;

namespace NotesScraper
{
    public class NotesCommunication
    {
        public  KeyValuePair<string, NotesViewResultSet[]>[] PullNotesView(string[] ViewNames, string server, string database, string password )
        {
            if (ViewNames == null || ViewNames.Length == 0 || ViewNames.ToList().Distinct().Count() != ViewNames.Length )
            {
                throw new ArgumentException();
            }
            else
            {
                List<KeyValuePair<string, NotesViewResultSet[]>> results = new List<KeyValuePair<string, NotesViewResultSet[]>>();
                NotesSession notesSession = new Domino.NotesSession();
                notesSession.Initialize(password);
                NotesDatabase notesDatabase = notesSession.GetDatabase(server, database, false);
                for(int i=0; i<ViewNames.Length; i++)
                {
                    List<NotesViewResultSet> result = new List<NotesViewResultSet>();
                    Domino.NotesView notesView;
                    string view = ViewNames[i];
                    notesView = notesDatabase.GetView(view);
                    NotesViewEntryCollection notesViewCollection = notesView.AllEntries;
                    for (int rowCount = 1; rowCount <= notesViewCollection.Count; rowCount++)
                    {
                        NotesViewEntry viewEntry = notesViewCollection.GetNthEntry(rowCount);
                        NotesDocument document = viewEntry.Document;
                        Array notesThings = document.Items as Array;
                        for (int j = 0; j < notesThings.Length; j++)
                        {
                            NotesItem notesItem = (notesThings.GetValue(j) as Domino.NotesItem);
                            result.Add(new NotesViewResultSet() 
                            { 
                                RecordID = rowCount,
                                Name = notesItem.Name,
                                Value = notesItem.Text
                            });
                        }
                    }
                    results.Add(new KeyValuePair<string,NotesViewResultSet[]>(view, result.ToArray()));
                }
                return results.ToArray();
            }
        }
    }
    public class NotesViewResultSet
    {
        public int RecordID {get;set;}
        public string Name { get; set; }
        public string Value { get; set; }
    }
}


来源:https://stackoverflow.com/questions/9058478/access-calendar-data-lotus-notes-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!