4 Replies Latest reply on Aug 14, 2018 12:21 AM by sunil@italentcorp.com

    User name from JavaScript in hosted v9

    stevewheeler

      Having tried a number of ways to get the current user name and all have been unsuccessful, turning to the community:

       

      Tried this and many variations:

      parent._jive_current_user.username;

      Access is denied

       

      also tried v3 api and osapi no love...

       

      This needs to work for a regular user and getting the identity of the current user to pass to a 3rd party application.

       

      Any global object that can be reference to get this? or API calls?

       

      Sincerely....

        • Re: User name from JavaScript in hosted v9
          john_reynolds

          Hi Stephen, here's how we do it. There are two ways, depending on if it's REST or OSAPI

           

          OSAPI:

           

          //JavaScript API request
          osapi.jive.corev3.people.getViewer().execute( user => console.log(user.name))

           

          REST

           

          //Regular REST request
          fetch('/api/core/v3/people/@me', {
            credentials: 'include',
            method: 'get'
          }).then(response => {
            response.text().then(res => {
              try {
                const data = JSON.parse(res.replace(/throw 'allowIllegalResourceCall is false.';/, ''))
                console.log(data.name)
              } catch (err) {
                console.warn('error while parsing user object: ', err)
              }
            })
          }).catch(err => {
            console.warn('Error while fetching user data: ', err)
          })
          • Re: User name from JavaScript in hosted v9
            Jeff Shurtliff

            This is how we do it from within a widget or tile (since they get sandboxed in an iframe):

             

            <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
            
            <p id="pageInfo"></p>
            
            <script>
            var userID = window.parent._jive_current_user.ID;
            var fullName = window.parent._jive_current_user.displayName;
            var username= window.parent._jive_current_user.username;
            var containerID = window.parent.jive.global.containerID;
            var placeID = window.parent.jive.global.containerBrowseId;
            var placeName = window.parent.document.getElementsByClassName("j-placeName").innerHTML;
            var pageTitle = window.parent.document.title;
            document.getElementById("pageInfo").innerHTML = 'User ID: ' + userID +
            '<br />Full Name: ' + fullName +
            '<br />Username: ' + username +
            '<br />User Type: ' + window.parent.userType +
            '<br />Container ID: ' + containerID + 
            '<br />Place ID: ' + placeID +
            '<br />Place Name: ' +  placeName +
            '<br />Page Title: ' + pageTitle;
            </script>
            
            1 person found this helpful