WAB get Portal user groups when map loads

2245
13
10-10-2017 09:13 AM
RajeswariBalakrishnan
Esri Contributor

I want to get the portal user and their associated groups when the map loads in Web app builder. In the previous version of WAB there was function getUserGroups to get the portal user group names. I am not able to find a similar function in the latest WAB. Has anyone tried to get Groups of portal user? Any suggestions is greatly appreciated.

Thanks,

RB

Tags (2)
0 Kudos
13 Replies
MansiShah
New Contributor III

I put this code in a startup function in the Legend widget and while I am not getting an error, I am also not able to get the username that I signed in with. It is just logging "undefined." So the username is undefined but I am signing in with my username and password. Any idea what could be causing that? Also when typing "this.portal" from line 2, "portal" is not one of my options, it's either portalUtils or portalUrlUtils as those are the only 2 files in jimu.js regarding portal. But the main issue is that I am getting undefined as the username even though when I open up the application, there is a sign on page where I input my username and password. 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Mansi,

  This.portal is a widget level var you are assigning the result of getPortal to.

Taking the code that is at the start of this thread and putting it into the startup function of any widget (after adding the 2 requires to the widget) produced the AGOL username in the console for me ever time.

        setTimeout(lang.hitch(this, lang.hitch(this, function(){
          var portalUrl = portalUrlUtils.getStandardPortalUrl(this.appConfig.portalUrl);
          var portal = portalUtils.getPortal(portalUrl);

          portal.getUser().then(lang.hitch(this, function(user) {
            this.portalUser = user;
            console.info(this.portalUser);
          }));
        })), 1000);
MansiShah
New Contributor III

It works now. Adding the two lines before portal.getUser() helped. this.portalUser gives back a helpful JSON with a lot of information and this.portalUser.username gives me the exact username I signed in with. 

THANK YOU SO MUCH.!!!

0 Kudos
JonathanMori1
New Contributor III

Hello, I really would like to get this solution working, but I keep receiving the error "Cannot read property 'getPortal' of undefined" I can post code if needed, but I basically did exactly what was presented in this article.

0 Kudos