Differentiating between hosted and reference service items in ArcGIS Online and Portal with the ArcGIS Python API

1134
6
Jump to solution
03-10-2021 01:52 PM
AnnaGabrielli
New Contributor II

Hello,

I am creating a program using the ArcGIS Python API to go through all content within my organization (ArcGIS Online and Portal) and "audit" it: returning information like the item ID, how it is shared, any groups it is in, whether or not it is delete protected, created and modified dates, etc.

I would like to also know whether content is hosted within my organization or if it is a referenced service layer. Is there a way to do this with the API? There does not seem to be a corresponding item property, the same way there is item.id, item.url, etc, and I have not been able to find any documentation on how to find this.

Thanks for any help or documentation links!

0 Kudos
2 Solutions

Accepted Solutions
FrédéricPRALLY
Esri Contributor

Hi Anna,

You can list all feature layers hosted using gis.admin module.

See sample below:

from arcgis.gis import GIS
gis = GIS("https://votreNomDomaine/portal", "login", "password")

server = gis.admin.servers.list()[0]
count=1
for service in server.services.list('Hosted'):
    print('{} {}'.format(count, service)) 
    count+=1

 Hope this help you,

Best regards,

Fred

View solution in original post

FrédéricPRALLY
Esri Contributor

Hi Anna,

Another more elegant code is to check if the property typeKeywords of the item has the text 'hosted Service' include in his list.

Retrieve code below:

from arcgis.gis import GIS
gis = GIS("https://votreNomDomaine/portal", "login", "password")

items = gis.content.search('*',item_type="Feature Layer", max_items=25)
itemsHosted = [item.title for item in items if 'Hosted Service' in item.typeKeywords]
print(itemsHosted)

 

Hope this help you,

Best regards,

Fred

View solution in original post

0 Kudos
6 Replies
jcarlson
MVP Notable Contributor

I'm assuming that you mean a layer you've added from another org, not a basemap reference layer?

To find out where the layer is originating from, you can call the url property on most operational layers.

lyrs = gis.content.search('some search for layers')

for lyr in lyrs:
    print(l.url)

If a content item isn't a service, you'll just get None, but otherwise, you'll get the origin URL of the service.

jcarlson_0-1615414743591.png

Does that help?

- Josh Carlson
Kendall County GIS
0 Kudos
AnnaGabrielli
New Contributor II
Thanks for your reply! When I do an item search and return some list lyrs:
for lyr in lyrs:
lyr.url

This will return the url for service layers, as you say. For example urls
for Feature service layers, or the external url for like a WMS layer.
While it is easy to see which is an internal url (Hosted feature service)
and which is an external one (WMS service), is there a way to determine
this programmatically? I want to differentiate between feature services
that are hosted in my organization and reference layers that are also
services.
0 Kudos
jcarlson
MVP Notable Contributor

I suppose if you stored your hosting server as an attribute, you could simply check

my_url = 'some-server-url'

if lyr.url == my_url:
print('internal')
else:
print('external')

Are you doing this on AGOL or a private Portal? There are some differences in what we're able to do depending on which.

I have noticed that the most recent update to AGOL added this sort of thing:

jcarlson_0-1615416713576.png

So there does seem to be some way to track whether an item is coming from an outside org...

- Josh Carlson
Kendall County GIS
0 Kudos
AnnaGabrielli
New Contributor II

Thanks for your replies! I found that one can determine if the item type matches any possible hosted types (all potential types can be found here) and then look for the enterprise specific 'Hosted' directory in the url, see below:

potential_hosted_types = list('all potential types here')
#loop through the list to see if our item type is a match
for potential_type in potential_hosted_types:
   if item.type in potential_type:
      maybe_hosted = True
   else:
      maybe_hosted = False

#See if the item is in the Hosted directory
if maybe_hosted == True:
   if 'Hosted' in item.url:
      item_hosted = True
   else:
      item_hosted = False
else:
   item_hosted = False

I am not sure how to do something similar for items in ArcGIS online since there is no Hosted directory.

0 Kudos
FrédéricPRALLY
Esri Contributor

Hi Anna,

You can list all feature layers hosted using gis.admin module.

See sample below:

from arcgis.gis import GIS
gis = GIS("https://votreNomDomaine/portal", "login", "password")

server = gis.admin.servers.list()[0]
count=1
for service in server.services.list('Hosted'):
    print('{} {}'.format(count, service)) 
    count+=1

 Hope this help you,

Best regards,

Fred

FrédéricPRALLY
Esri Contributor

Hi Anna,

Another more elegant code is to check if the property typeKeywords of the item has the text 'hosted Service' include in his list.

Retrieve code below:

from arcgis.gis import GIS
gis = GIS("https://votreNomDomaine/portal", "login", "password")

items = gis.content.search('*',item_type="Feature Layer", max_items=25)
itemsHosted = [item.title for item in items if 'Hosted Service' in item.typeKeywords]
print(itemsHosted)

 

Hope this help you,

Best regards,

Fred

0 Kudos