How do I get the signed-in username in ArcGIS Pro in C#/.NET SDK?

445
3
Jump to solution
08-04-2021 11:40 AM
ScottLehto3
Occasional Contributor

Can anyone tell me how to get the signed-in username in ArcGIS Pro using C#/.NET SDK?

0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

You can get the signed in user name for the current active portal, but only when the user signed in.

QueuedTask.Run(() => {
  var portal = ArcGISPortalManager.Current.GetActivePortal();
  if (portal != null) {
    // make sure is signed in
    var IsSignedOn = portal.IsSignedOn();
    if (!IsSignedOn) portal.SignIn();
    var signOnUserName = portal.GetSignOnUsername();
    // ...
  }
});

 

View solution in original post

3 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

You can get the signed in user name for the current active portal, but only when the user signed in.

QueuedTask.Run(() => {
  var portal = ArcGISPortalManager.Current.GetActivePortal();
  if (portal != null) {
    // make sure is signed in
    var IsSignedOn = portal.IsSignedOn();
    if (!IsSignedOn) portal.SignIn();
    var signOnUserName = portal.GetSignOnUsername();
    // ...
  }
});

 

ScottLehto3
Occasional Contributor

Side Question: Can you run QueuedTask.Run in the Module1.cs? I ask because I received advice to have my business logic in the Module.cs to stay organized.

QueuedTask.Run(() =>

 

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

You can call QueuedTask.Run anywhere.  Putting business logic into the Module class makes sense if you need access to your logic from various tools and buttons from within your add-in.  Also note that you can access your Module class (which is a singleton) from any other add-in or managed configuration through  FrameworkApplication.FindModule("MyAddIn_Module"). 

In your business logic make sure that you don't 'nest' calls to QueuedTask.Run, meaning that you should not call a function that calls QueuedTask.Run from within the context of another QueuedTask.Run action.

If you write re-usable functionality that is calling Pro API methods that require to run from within the context of QueuedTask.Run make sure to put the following comment in your summary description of your method:

This method must be called on the MCT.

The Pro API is using the same pattern for methods that need to run from the MCT.