Running Lotes Notes agent from .NET

I'm referencing domobj.tlb from my Visual Studio project to access LotusNotes objects. I am getting an "Object reference not set to an instance of an object" when I try to do notesAgent.RunOnServer(documentId). Please refer to the code below:

The program doesn't have any problem in saving the parameter document in step 8. So connection, username / password and other things are not the issue. When it hits step 10, it's throwing the standard .NET NullReferenceException "Object reference not set to an instance of an object". BTW, I am using C# but I don't believe the result from VB.NET would be any different.

I am kind of stuck here. Your expert opinion will be highly appreciated. I'm giving this question 500 points as I've not made any advance in past couple of days and the deadline is very near.

The first "NotesDatabase" is the class name which I am instantiating. The second "notesDatabase" is the name of the instantiated object. This code is in C# which is case sensitive. My code could well have been
04 NotesDatabase ndb = dbDir.OpenDatabase(DatabaseName, false);
05 NotesDocument ndoc = ndb.CreateDocument();
06 ndoc.ReplaceItemValue("Form", FormName);
07 ndoc.ReplaceItemValue("EmailAddress", "abc@xyz.com");
08 ndoc.Save(true, false, false);
09 NotesAgent agent = ndb.GetAgent(AgentName);
10 agent.RunOnServer(ndoc.NoteID);
and it would not have made any difference.

Thanks for your response. However, please bear in mind that I am referencing a COM object. So lots of stuff that can are available in Lotus Scripts is not available here. ndoc.NoteID is a string and not an array. It's returning with the actual ID of the document created. I've also verified that the agent exists. I've supplied only relevant portions of my code. The parameter list of the method includes the following:

1. The database log shows a session has been accessed. But the database log is silent about the agent. The agent log is empty. It seems that the agenat has not run at all.
2. Yes, the agent expects the parameter document.
3. Yes, the ID I am using is a manager and has all the security permissions.
4. All our agents are on server.
5. In step 09, I can watch the agent and can view it's properties. So no, the agent is NOT NULL. However, when I invoke RunOnServer (step 10), I immediately get an exception without the agent being run.

Thanks, yes, I've verified that ndoc has a NoteID which matches with the document ID in databse. It failes on line 11. Neither ndoc nor agent is null. So it's basically something in RunOnServer method which throws an exception (prior to actually running the agent). So I guess my question is, what information it needs to see in the NotesAgent object? The exception doesn't tell much, neither the stacktrace goes deep within.

Can you leave out the noteID, and call agent.RunOnServer without any parameters? Oh forget it, it won't make any difference...

And when you call agent.Run() ??

I suppose (and hope) that the agent's properties in the Designer will allow it to run in the background? It must be a scheduled agent, run never will do. Can you ask someone to call the same agent from LotusScript, also with RunOnServer, to make sure it does run at all?

The COM reference doesn't have a parameterless method signature. So I can't try RunOnServer without NoteID. I've tried Run(NoteID) with no avail. Again, the parameterless method signature is not available for "Run" as well. Yes, the agent can be run within LotusScript with RunOnServer without any issues. This agent is not a scheduled agent. It must be invoked by my process to complete the action (based on my parameter document).

Instead of the NoteID parameter to indicate your document, you could do it the difficult way, using an additional field and a view that selects documents with that field and a specific value. You might call agent.RunOnServer(""). But that's not the issue.

I think I'll use the night (it's 01:22 over here) for some reflection... ;-)

You've been very helpful. I've thought of different ways to adress my issue. For example, I can save the document only and not run the agent from .NET. I can make the agent a scheduled agent which will pick up the documents and act upon it. But that's not the point. We're developing a model which will be used by lot of different applications and at some point or the other I'll be required to run the agent from .NET passing a parameter document. Proposion does it (I've used Proposion to pass in the emailaddress parameter to the agent and it runs from .NET but my company would not buy the license), so why can't we?

If you've access to .NET and Notes tools and more importantly some spare time, you can try my code. If you're successful, I can blame it on our environment :-)

Quick note: For COM applications, the Notes program directory must be in the application path to use runonserver.

I wonder if you create the newdocument with a specific "form" name, and then have a regularly scheduled lotus script agent act on those documents and when done with the processing rename the form, then all your com agent has to do is create the document with the "form" name, and your runonserver agent can be modified to step through and process those "forms" at regular intervals. If it doesn't find collection, it just exits and writes to a log.

Also, the runonserver agent will write to the domino log: log.nsf on the server.