Quick-n-Dirty: @ClientType() in XPages

The @ClientType formula provides an interesting behaviour: If you add the value to a label, the result will be as expected. It returns “Web” in Browser and returns “Notes” in XPiNC.

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
   <xp:label value="#{javascript:@ClientType()}" id="label1" />
</xp:view>

But if you add it to a computed field on a form and use this form in a datasource, the result in XPiNC is not „Notes„, it is „None“ instead. In the following example, the form „Test“ has a computed field named „ClientType„. The value of the field is @ClientType.

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">

   <xp:this.data>
      <xp:dominoDocument var="document1" formName="Test" 
         computeWithForm="onload">
      </xp:dominoDocument>
   </xp:this.data>

   <xp:label value="#{javascript:@ClientType()}" id="label1" />
   <xp:br />
   <xp:label id="label2">
      <xp:this.value>
         <![CDATA[#{javascript:
            document1.getItemValueString("ClientType")
         }]]>
     </xp:this.value>
   </xp:label>
</xp:view>

[The computeWithForm property of the datasource has to be set to „onload“ or „onsave„. If set to „onboth“ this will not work.]

This behaviour gives you control about the subforms which are used in the form, depending of the client and/or technology. Just add a computed subform to your form and add a @Formula like this:

@If(
    @ClientType="Web";"WebAccess";
    @ClientType="Notes";"NotesAccess";
    "XPiNCAccess"
)

This allows you to add specific fields to a document, or can help to identify the way how the document was created. If you add a field in the querySave event on the XPage, you can identify if this document was created in the web by XPage or old-school web access of the form.

<xp:this.data>
   <xp:dominoDocument var="document1" formName="Test"
      computeWithForm="onsave">
      <xp:this.querySaveDocument>
         <![CDATA[#{javascript:
            document1.replaceItemValue("ClientTypeXPage", "1"))
         }]]>
      </xp:this.querySaveDocument>
   </xp:dominoDocument>
</xp:this.data>

By extending the subform computation, you can use a subform for every type of access:

@If(
    @ClientType="Web" & ClientTypeXPage="1";"XPageWebAccess";
    @ClientType="Web";"WebAccess";
    @ClientType="Notes";"NotesAccess";
    "XPiNCAccess"
)
Dieser Beitrag wurde unter @Formula, Allgemein, ServerSide JavaScript, Web, XPages abgelegt und mit , , , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.