How can I retrieve the portal user name in a custom WAB widget?

3864
6
Jump to solution
09-28-2015 01:35 PM
BenFerguson
New Contributor III

I'm building a custom widget in WAB.  I need to retrieve the user name of the user logged into the Portal.  Is there a property or function in WAB somewhere that can provide that?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
HerdisGudbrandsdottir
New Contributor III

Hi Ben,

You can define portalUtils in widget and access user through the portal instance:

define([

    'jimu/BaseWidget',

  'jimu/portalUtils',

    'dojo/_base/declare'

  ],

function(BaseWidget, portalUtils, declare) {

...

this.portal = portalUtils.getPortal(this.appConfig.portalUrl);

username = this.portal.user.username;

...

See also:

https://developers.arcgis.com/javascript/jsapi/portal.html

https://developers.arcgis.com/javascript/jsapi/portaluser-amd.html

View solution in original post

0 Kudos
6 Replies
HerdisGudbrandsdottir
New Contributor III

Hi Ben,

You can define portalUtils in widget and access user through the portal instance:

define([

    'jimu/BaseWidget',

  'jimu/portalUtils',

    'dojo/_base/declare'

  ],

function(BaseWidget, portalUtils, declare) {

...

this.portal = portalUtils.getPortal(this.appConfig.portalUrl);

username = this.portal.user.username;

...

See also:

https://developers.arcgis.com/javascript/jsapi/portal.html

https://developers.arcgis.com/javascript/jsapi/portaluser-amd.html

View solution in original post

0 Kudos
BenFerguson
New Contributor III

Thank you Herdis.

I hadn't found the portalUtils.getPortal() function.  That is what I needed to get a reference to the portal instance the WAB is using.

I wish WAB was better documented.

Alexwang
Occasional Contributor II

Does this work for in-panel widgets?

0 Kudos
MansiShah
New Contributor III

I am getting a: " Uncaught TypeError: Cannot read property 'portalUrl' of undefined " error when I use this code. When I don't have this code, my template loads correctly without any errors and I do have the correct portalUrl in place??

0 Kudos
NathanB
New Contributor

did you ever figure this out?  having the same issue.  sometimes i think people find it funny to post incomplete examples instead or withhold which version of the api they are using.

0 Kudos
HerdisGudbrandsdottir
New Contributor III

I can't say how much has changed in five years - but I suggest you to debug it by looking into the properties of 'this' in this.appConfig.portalUrl - since the error message implies that appConfig is null - maybe this has changed since 2015? Or maybe the 'this' in your script point to something else? Anyway, good luck.

0 Kudos