WAB get Portal user groups when map loads

1232
13
10-10-2017 09:13 AM
RajeswariBalakrishnan
New 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
RobertScheitlin__GISP
MVP Esteemed Contributor

RB,

   You would use the portalUtils class in jimu:

...
  'jimu/portalUtils',
  'jimu/portalUrlUtils',
...
portalUtils, portalUrlUtils,
...
      var portalUrl = portalUrlUtils.getStandardPortalUrl(this.appConfig.portalUrl);
      var portal = portalUtils.getPortal(portalUrl);

      portal.getUser().then(lang.hitch(this, function(user) {
        this.portalUser = user;
        ....
0 Kudos
RajeswariBalakrishnan
New Contributor

Thanks for the quick reply Robert. But when i try this inside the LayoutManager.js _onMapLoaded function, it throws an error stating "Credential is null"

Thanks,

RB

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Sounds like a timing issue. Have to tried to use a setTimeout for a couple of seconds and see if that works?

0 Kudos
MansiShah
New Contributor III

I'm not sure why I keep getting a portalUrl not defined issue even though I have the correct portalUrl in my WAB template's config file? I keep getting this error whether I use code in this thread or if I use code in the following thread: https://community.esri.com/message/740450-re-how-can-i-retrieve-the-portal-user-name-in-a-custom-wab... 

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Mansi,

   Sounds like a scope issue. You need to use lang.hitch to maintain your code blocks scope.

0 Kudos
MansiShah
New Contributor III

I still get the same issue with portalUrl. I am writing my code in WidgetManager.js. It contains the required imports for portalUtils in the define and function sections. I still get the same error even if I model my code similar to (using portalUtils to get current logged user won't work for off-panel widgets ) This is my current code (modeled after https://community.esri.com/message/555547?commentID=555547#comment-555547) :

var func = lang.hitch(this, function(){

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

   var username = this.portal.username;

   console.log(username);

});

func();

........

This code is written right before the last return statement in WidgetManager.js. I get the portalUrl error even if I place the code in a widget such as WMXLegend/Widget.js.

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Mansi,

   I am not sure what you are doing wrong in your code then. Here is another thread that might be easier to follow:

https://community.esri.com/thread/185734-using-portalutils-to-get-current-logged-user-wont-work-for-... 

0 Kudos
MansiShah
New Contributor III

I've tried this code as well. I am still getting the same error:

Uncaught TypeError: Cannot read property 'portalUrl' of undefined
at WidgetManager.js?wab_dv=2.6:1261
at ia (init.js:28)
at ia (init.js:27)
at ia (init.js:27)
at ia (init.js:27)
at init.js:28
at ja (init.js:28)
at ga (init.js:28)
at f (init.js:30)
at HTMLScriptElement.<anonymous> (init.js:35)

Many of the methods mentioned/used in the examples in various threads do not show up as options for me in VScode as an option. Methods such as getStandardPortalUrl from the above example. I get an error saying those functions are not defined in the console when I run them also. 

The current code I tried is: 

setTimeout(lang.hitch(this, function(){
this.portal = portalUtils.getPortal(this.appConfig.portalUrl);
this.username = this.portal.user.username;
console.info(this.username);
}), 1000);

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Mansi,

  OK, that code should be used in a widget and in a function like the startup function:

setTimeout(lang.hitch(this, lang.hitch(this, function(){
  this.portal = portalUtils.getPortal(this.appConfig.portalUrl);
  this.username = this.portal.user.username;
  console.info(this.username);
})), 1000);
0 Kudos