简体   繁体   中英

how to insert a new notes document in the domino server with c# and the usage of interop.domino.dll?

This is my first question here.

I want to know the syntax to insert a new notes document in the lotus notes database if it is not existing using c#.
I have a code in vb script bit I do not know about vb script and lotus notes.

     set doc = vw.GetDocumentByKey(empno)

        if doc is nothing then
          set doc = db.CreateDocument
          doc.Form = "EmployeeRepository"
          doc.Employno = empno
          doc.FirstName = fname
          doc.LastName = lname
          doc.Group = grp
          doc.Department = dept
          doc.officeemailaddress = officemail
          doc.officegeneralline = officegenline
          doc.designation = desig
          doc.officeaddress = officeadd 
        else
          doc.FirstName = fname
          doc.LastName = lname
          doc.Group = grp
          doc.Department = dept
          doc.officeemailaddress = officemail
          doc.officegeneralline = officegenline
          doc.designation = desig
          doc.officeaddress = officeadd     
        end if

        call doc.save(true, true)

How can I achieve this in c#?

The C# syntax for the if statement is different. Instead of this:

if doc is nothing then
  ...
else
  ...
end if

You will need

if (doc != null)
{
  ...
}
else
{
  ...
}

Also, the C# language does not support shorthand notation doc.item = X. So the assignments in that format in the above code need to be changed to use the ReplaceItemValue method. Ie, instead of this:

  doc.Form = "EmployeeRepository"
  doc.Employno = empno
  doc.FirstName = fname
  doc.LastName = lname

you need to use this:

  doc.ReplaceItemValue("Form","EmployeeRepository");
  doc.ReplaceItemValue("Employno",empno);
  doc.ReplaceItemValue("FirstName", fname);
  doc.ReplaceItemValue("LastName", lname);

I might also suggest trying an ExpandoObject (although I haven't tried that yet, but I'm about to give it a go). It's a dynamic type so you have to be careful with how you create it, but you could keep adding additional properties to it without having to instantiate them directly:

    dynamic noteDocument = new System.Dynamic.ExpandoObject();

    noteDocument.ShortName = "wonkaWillie";
    noteDocument.Comment = "No Comment";
    noteDocument.MailSystem = "Other";
    noteDocument.PowerLevel = "It's over NINE THOUSAND!!!!!";

I suppose you could just as easily (and probably be a little tighter of a solution) to have a pre-formatted class ready for the occasion of adding data into a specific document format too though.


So the ExpandoObject method works, but using a class with explicitly declared fields / properties is much cleaner....you can pass in an instance of a class to a method that performs this pretty handily:

   class NotesDocumentItemClass
    {
         public string Form {get; set;} = "Person";
         public string FullName {get; set;} = "Over 9000/Notes/Address/Or/Whatever";
    }

and then pass an instance of that class into a method like.....

    private bool AddEntry(NotesDatabase db, NoteDocumentItemClass d)
    {
            NotesDocument newDoc = db.CreateDocument();
            doc.ReplaceItemValue("Form", d.Person);
            doc.ReplaceItemValue("FullName", d.FullName);
            return newDoc.Save();

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM