Forum Discussion

tnotte's avatar
tnotte
Advisor
12 years ago

Returning user's full name in a component

This should be real easy, but I'm not having any luck with it yet.

 

In Studio, in a custom component, it's as easy as using this tag to return the logged in users login name: Welcome, ${user.login}

 

By using this, I'll get: Welcome, mstim

But I want to return the users full name; Welcome, Tim Notte

 

I don't see anything listed here:

http://lithosphere.lithium.com/t5/developers-knowledge-base/Context-objects-for-custom-components-user/ta-p/9339

 

I've been pointed in the direction of using REST API to get this.  In our community's Public Statistics, there is a Name feild, populated as expected from our SSO.  How do I grab that and use it in a component?

 

New to API,

Tim

3 Replies

  • SeanA's avatar
    SeanA
    Lithium Alumni (Retired)
    12 years ago

    Hi Tim, the context objects provide "shallow" information for a number of reasons including performance. They typically include the identifying data needed to make subsequent REST calls to get complete attributes for an object. In your example you would use the id or login available in the context object and then make a subsequent REST call to the user service: /users/id/${user.id}.

     

    This will return a "deep" set of attributes including the first and last name of the user. Look for:

     

    <profile name="name_first" type="string">FIRST NAME HERE</profile>
    <profile name="name_last" type="string">LAST NAME HERE</profile>

  • SeanA's avatar
    SeanA
    Lithium Alumni (Retired)
    12 years ago

    The code will look something like this (i'm a rookie when it comes to freemarker so my guess is there's a more elegant way to do this but this works):

     

    <#assign fullName=''/>

    <#assign aUser=rest("/users/id/${user.id}").user/>

    <#list aUser.profiles.profile as profileList>
    <#if profileList.@name=='name_first'>
    <#assign fullName=profileList/>
    </#if>
    <#if profileList.@name=='name_last'>
    <#assign fullName=fullName + ' ' + profileList/>
    </#if>

    </#list>

     

     

  • tnotte's avatar
    tnotte
    Advisor
    12 years ago

    Unfortunately, this did not work for me. However, we were very close! This is what did work in the end:

    <#assign firstname = rest("/users/id/${user.id}/profiles/name/name_first").value/>

    <#assign lastname = rest("/users/id/${user.id}/profiles/name/name_last").value/>

     

    That assignment worked, so using this, I could return the string values:
    <span>${firstname} ${lastname}</span>

     

    The results are just as I wanted. Looks so simple now!  :robothappy: Thanks!