0
votes

I've got a XPage containing a Rich Text Field. In this Rich Text Field is some Text, formatted with HTML. As long as I don't change anything, it is very easy to get the content using

docMail.getItemValue("dBody")[0].toString() 

The content of the document looks like this:

enter image description here

As soon as I change anything of the Text, the content will be saved as MIME and my code doesn't work anymore, because when I want to get the Value of dBody, it's empty. The content looks like this:

enter image description here

So it's MIME Part now. But how do I get the HTML Code? I just want the HTML Code and send it via Mail.

3
If docMail refers to a document data source (not a "back end" document), call getValue instead of getItemValue. Otherwise, use the wrapper function mentioned on the question Per included to get the document as a data source... and then call getValue instead of getItemValue. :)Tim Tripcony

3 Answers

2
votes

In your case: if you send the whole document (doc.send) it should work the very moment you rename your field from dBody to Body. If you want to work on the backend, then use doc.getMimePart to get to the body field. Or use Per's wrapper.

1
votes

Thanks guys, but I've been working on a solution which seems to do the job too (based on the XSnippet emailBean by Tony McGuckin. Here's the code:

Got a new Bean containing this code:

package de.esg.weiterbildung;
import lotus.domino.Database;
import lotus.domino.NotesException;
import lotus.domino.Session;
import com.ibm.xsp.model.domino.wrapped.DominoDocument;
import com.ibm.xsp.model.domino.wrapped.DominoRichTextItem;
import com.ibm.xsp.model.domino.wrapped.DominoDocument.AttachmentValueHolder;
import de.sit.xpagesutils.*;

public class HTMLHelper {
    private boolean debugMode = true;
    private String fieldName="dBody";
    private DominoDocument document;

    public String getFieldName(){
        return this.fieldName;
    }

    public void setFieldName(final String fieldName){
        this.fieldName = fieldName;
    }   

    public boolean isDebugMode(){
        return this.debugMode;
    }

    public void setDebugMode(final boolean debugMode){
        this.debugMode = debugMode;
    }
    public DominoDocument getDocument(){
        return this.document;
    }

    public void setDocument(final DominoDocument document){
        this.document = document;
    }

    public String getBodyHTML(String unid)throws NotesException{
        String back ="";

        if(null != document){
            if(this.isDebugMode()){
                System.out.println("Started getBodyHTML()");
            }
            final DominoRichTextItem drti = document.getRichTextItem(fieldName);
            if(null != drti){
                try {
                    String html = drti.getHTML();
                    if(this.isDebugMode()){
                        System.out.println("Completed getBodyHTML() : " + html);
                    }
                    return html;
                } catch (Exception e) {
                    if(this.isDebugMode()){
                        System.out.println("Failed getBodyHTML() : " + e.getMessage());
                    }
                }
            }
        }
        return back;
    }

}

And now I get the body by calling this code:

helper.setDocument(docMail);
helper.setFieldName("dBody");
var htmlBody:String = helper.getBodyHTML(unid);

Works so far!

1
votes

Here is a method I use to access a field which has HTML content, but may be stored either as plain text or as a rich text field. I do have the rich text fields set to store their contents as HTML and MIME.

String getRichTextField(Document document, String fieldName) {
  MIMEEntity me;
  try {
    me = document.getMIMEEntity(fieldName);
    String text;
    if (me != null) {
      text = me.getContentAsText();
    } else {
      @SuppressWarnings("unchecked")
      Vector<String> values = document.getItemValue(fieldName);
      text = join(values, "\n"); // join is a utility method that joins a vector of strings using the given delimiter
    }
    return text;
  } catch (NotesException e) {
    System.err.println("Error accessing field '" + fieldName + "'.");
    printStackTrace();
    return "error accessing field";
  }

}