How do I use ArcGISPortalManager?

1326
3
12-06-2016 08:23 AM
HoriaTudosie
Occasional Contributor II

I'm working with ArcGis Pro 1.4.0-alpha.3

My goal is to get a GeoDatabase or a Zip file produced in my extension and move it in MyContent on the portal.

I set up the portal in Project/Portals:

And I want to use that setting.

However, trying to use PortalManager (documented in ArcGIS Pro 1.3 API Reference Guide - How do I get the help for 1.4?) I'm told thet this object is obsolete and redirected to use ArcGISPortalManager, which shows up in the code as part of ArcGis.Desktop.Core, but has peculiarities: most of its methods (which look fine and I want to use,) do not work on the current thread, and do not work on the QueuedTask thread!

Starting with GetActivePortal, I try to do this (on a constructor of a button): 

QueuedTask.Run(() =>
{ 
   var a = ArcGISPortalManager.Current.GetActivePortal();
   IsChecked = a.IsSignedOn();
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

It does not work either with or without the QueryTask, neither in the constructor, nor in InClick event! (I have even tried in other parts of the extension, as in the initialization of the Module!)

As I've found some work-around to obtain the active portal and open it in the browser:

protected override void OnClick()
 {
#pragma warning disable 618
    var url = new UriBuilder(PortalManager.GetActivePortal())
    {
       Path = "home/content.html"
    };
#pragma warning restore 618
   Process.Start(url.Uri.AbsoluteUri);
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This code mixes the obsolete PortalManager with the new one (as in):

#pragma warning disable 618
 m_portal = ArcGISPortalManager.Current.GetPortal(PortalManager.GetActivePortal());
#pragma warning restore 618‍‍‍

And does not let me use the wonderful methods of ArcGISPortal objects (m-portal) for the treading issues already mentioned!

Tags (1)
0 Kudos
3 Replies
CharlesMacleod
Esri Regular Contributor

Your code, as such, looks ok. The alpha 3 snapshot likely has bugs. You should have access to the alpha 4 snapshot otherwise please re-try on 1.4 final (available dec, jan).

0 Kudos
HoriaTudosie
Occasional Contributor II

Hi Charles,

Sure the Alpha may have bugs and I'll wait the new release to continue.

However, my code does not look OK since it uses a pragma to enable an obsolete object.

The issues I have are:

1. When the obsolete object will be purged from the next version, the code won't work anymore,

2. All documentation I have found are only for the obsolete object (PortalManager) and nothing for the new recommended object (ArcGisPortalManager,)

3. (This may get fixed in next releases, but) some of ArcGisPortalManager methods do not work on any thread...

Tx,

0 Kudos
CharlesMacleod
Esri Regular Contributor

Gotcha,

Ok so:

1. When the obsolete object will be purged from the next version, the code won't work anymore

The obsolete class will still work at 1.4 (we have to keep it working or else it would break the API). It will be deprecated at the next major release which is a "2.0", not a 1.5, 1.6, 1.x, etc. which would be considered minor releases

2. All documentation I have found are only for the obsolete object (PortalManager) and nothing for the new recommended object (ArcGisPortalManager,)

Alpha snapshots do not have doc so, yes, you are definitely on the bleeding edge on a snapshot. I have copy/pasted a few things below. The samples will be switched to use ArcGISPortalManager, ArcGISPortal with EsriHttpClient and not deprecated class(es) at 1.4. Documentation is released with the final version.

3.  (This may get fixed in next releases, but) some of ArcGisPortalManager methods do not work on any thread...

Definitely sounds like a bug in the alpha3. ArcGISPortalManager.Current and ArcGISPortal instances can be used on any thread at 1.4 final. Also, upgrade your snapshot to alpha4 if that is available.

>>>>>>>>>>>>>>>>>>

ArcGISPortalManager: Get the Current Active Portal

var active_portal = ArcGISPortalManager.Current.GetActivePortal();
string uri = active_portal.PortalUri.ToString();

ArcGISPortal: Get the Current Signed-on User

var user = active_portal.GetSignOnUsername();

ArcGISPortalManager: Get a list of all your Portals

var portals = ArcGISPortalManager.Current.GetPortals();
//Make a list of all the Uris
var portalUris = portals.Select(p => p.PortalUri.ToString()).ToList();

ArcGISPortalManager: Add a portal to the list of portals

var portalUri = new Uri("http://myportal.esri.com/portal/", UriKind.Absolute); 
ArcGISPortalManager.Current.AddPortal(portalUri);

ArcGISPortalManager: Get a portal and Sign In, Set it Active

//Find the portal to sign in with using its Uri...
var portal = ArcGISPortalManager.Current.GetPortal(new Uri(uri, UriKind.Absolute));
if (!portal.IsSignedOn()) {     
  //Calling "SignIn" will trigger the OAuth popup if your credentials are    
  //not cached (eg from a previous sign in in the session)    
  if (portal.SignIn().success) {         
      //Set this portal as my active portal        
      ArcGISPortalManager.Current.SetActivePortal(portal);     
   } 
}

ArcGISPortalManager:Listen for the Portal Events

ArcGIS.Desktop.Core.Events.ActivePortalChangedEvent.Subscribe((args) => {      
   var active_uri = args.ActivePortal?.PortalUri.ToString();     
   //etc
});  
ArcGIS.Desktop.Core.Events.ArcGISPortalAddedEvent.Subscribe((args) => {     
    var added_portal = args.Portal;     
   //etc
});  
ArcGIS.Desktop.Core.Events.ArcGISPortalRemovedEvent.Subscribe((args) => {     
   var old_uri = args.RemovedPortalUri;     
  //etc
});