Jaspersoft BI Tools

iReport,Jaspersoft Design Studio,Jaspersoft Server, Schema Workbench,Analysis,Dashboards, Self Service BI, Domains,Topics,Ad-Hoc Reports, Jaspersoft ETL, Embedding BI,HTML-5 charts, Fusion Charts,JFree Charts,Drill down reports , Drill down Dashboards,Visualize.js, Performance Tuning, D3 Charts, Jaspersoft Customization

Tuesday, October 13, 2015

TIP: LoggedIn User Name defaut value expression in Jasper Server 6.x

How to get the default value of logged in user in jasper server input control.

1) Create a parameter named with "LoggedInUser" (Don't give double quotes).
    Give type as : com.jaspersoft.jasperserver.api.metadata.user.domain.User
NOTE:
# LoggedInUser is a default parameter provided by the product which is not visible. We have to manually create and use.
# Do not give this parameter is for prompting ( i.e., deselect the check box).

2) Create another parameter of your choice ( I have created "User" as another parameter).
Give type as : java.lang.String
Give default value expression as : $P{LoggedInUser}.getUsername()

3) Publish the report to the server and check the text field poped up with logged In User Name

Test Image : 


 

References : 
http://community.jaspersoft.com/wiki/built-parameters-logged-user

JRXML (Copy paste the code in Professional 6.x Studio report and observe the parameters and expressions). 


<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.1.0.final using JasperReports Library version 6.1.0  -->
<!-- 2015-10-13T23:55:04 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="UserName" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="83eecdc9-c7c8-4d1a-9508-923092906537">
    <property name="ireport.jasperserver.url" value="http://localhost:8080/jasperserver-pro/"/>
    <property name="ireport.jasperserver.user" value="superuser"/>
    <property name="ireport.jasperserver.report.resource" value="/Training/UserName_files/main_jrxml"/>
    <property name="ireport.jasperserver.reportUnit" value="/Training/UserName"/>
    <parameter name="LoggedInUser" class="com.jaspersoft.jasperserver.api.metadata.user.domain.User" isForPrompting="false"/>
    <parameter name="User" class="java.lang.String">
        <defaultValueExpression><![CDATA[$P{LoggedInUser}.getUsername()]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="120" y="0" width="120" height="30" uuid="b4ccca7e-ad0e-4202-85f4-a4117b1a7c57"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="12" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{User}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="0" width="120" height="30" uuid="dcbe87b3-fb60-488b-a959-4317df54299f"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="12" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["User Name :"]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>