XPages and Me

My Journey into XPages Development

sessionAsSigner Oddities – Part 1

During a recent project, I had to start using sessionAsSigner in my SSJS.

For those of you, who don’t know what sessionAsSigner does: It allows a current user, to run code and do things he usually wouldn’t be allowed to do, due to his access level for a certain application. sessionAsSigner runs the code with the access level of the signer of the design element. So, whoever touched the XPage or Script Library last in the designer, this person’s access rights will be used for sessionAsSigner. That means, if you roll an application out to production, make sure all design elements are being signed with the admin ID.

Though it works very well, I also ran into some odd behavior while using it. I am not sure if this is going to be a whole series (as I don’t know if I’ll encounter any more issues), but I thought I’ll start it out as one, just in case.

So, what happened? In this particular instance, I wanted to read the content of a rich text field as MIME Entity. I’ve used that in several other projects and it worked very well.

Note: If you want to store the content of a rich text field as MIME Type, you need to make sure to check the box “Store contents as HTML and MIME” in the field’s properties:

Image 1

However, using it while also using sessionAsSigner to access the document that contained the rich text field, would always return MIME entity as null. Here is some sample code, on how I tried to access the rich test field:

//Declarations
var oDBCurrent:NotesDatabase = sessionAsSigner.getDatabase(session.getServerName(), session.getCurrentDatabase().getFilePath());
var oVWMailProfiles:NotesView = oDBCurrent.getView('$vwSYSLookupEmailProfiles');
var oDocMailProfile:NotesDocument = oVWMailProfiles.getDocumentByKey('.MailTemplate', true);

//Get body text
var oItem:NotesItem = oDocMailProfile.getFirstItem("Body");
var entity:NotesMIMEEntity = oItem.getMIMEEntity();

This was very odd because, when checking the document properties of the mail template, I could see that the data type of the rich text field was indeed MIME Part:

Image 2

What was the difference between my previous projects where I used it and now? Come to fine out, it was sessionAsSigner. In all of my previous projects, I always accessed the current database with “database”:

//Declarations
var oVWMailProfiles:NotesView = database.getView('$vwSYSLookupEmailProfiles');
var oDocMailProfile:NotesDocument = oVWMailProfiles.getDocumentByKey('.MailTemplate', true);

//Get body text
var oItem:NotesItem = oDocMailProfile.getFirstItem("Body");
var entity:NotesMIMEEntity = oItem.getMIMEEntity();

So, what did solve the problem? It was actually really simple. All I had to do was to add the following to the beginning of my code:

sessionAsSigner.setConvertMime(false);

And my final code looked like this:

sessionAsSigner.setConvertMime(false);

//Declarations
var oDBCurrent:NotesDatabase = sessionAsSigner.getDatabase(session.getServerName(), session.getCurrentDatabase().getFilePath());
var oVWMailProfiles:NotesView = oDBCurrent.getView('$vwSYSLookupEmailProfiles');
var oDocMailProfile:NotesDocument = oVWMailProfiles.getDocumentByKey('.MailTemplate', true);

//Get body text
var oItem:NotesItem = oDocMailProfile.getFirstItem("Body");
var entity:NotesMIMEEntity = oItem.getMIMEEntity();

Thanks to everyone who responded to my stackoverflow question regarding this issue and helped me to figure it out. You can find the question here.

Advertisements

2 responses to “sessionAsSigner Oddities – Part 1

  1. Oliver Busse February 13, 2015 at 5:35 pm

    I still believe that this is needed only if you refer your document directly via SSJS. If you get it via a panel’d datasource the conversion is turned off by default – just a guess, but my example in the SO question doesn’t need that line. Nevertheless: great it worked out for you that way 🙂

    • Daniel Friedrich February 13, 2015 at 5:43 pm

      Thank you Oliver, and thank you for your response to my SO question, I appreciate the help.

      Would be interesting to find out regarding the panel’d datasource. I might give that a try and see, how it goes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: