Search/Find maps that contain layer

1618
4
Jump to solution
06-20-2021 07:08 PM
LindsayRaabe_FPCWA
Occasional Contributor III

So I've googled and searched and thought I'd just ask. Is there any way to search through all the maps in an organisation to find which ones contain a particular layer? 

I ask as I want to replace a layer with a completely new one (not overwritten) and that means updating all the maps that contained the old one - but I have to open them all individually. Surely there's an easier way?

Lindsay Raabe
GIS Officer
Forest Products Commission WA
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

One Fell Swoop

If you have a lot of maps to update, or you just trust your coding and don't require feedback and interaction for each change, this will update every map that has the layer you're replacing in one go.

from arcgis.gis import GIS
from arcgis.mapping import WebMap

gis = GIS('your-org-url', 'username', 'password')

# Don't forget to go all the way to the layer index, i.e., 'FeatureServer/0'!
old_url = 'url-to-your-old-layer'
new_url = 'url-to-your-new-layer'

# Find all maps in org
webmaps = gis.content.search('', item_type='Web Map', max_items=-1)

# Iterate over operational and basemap layers, updating URL
for w in webmaps:
    wm = WebMap(w)
    for b in wm.basemap.baseMapLayers:
        try:
            if b.url == old_url:
                b.url = new_url
        except AttributeError:
            continue
                
    for l in wm.layers:
        try:
            if l.url == old_url:
                l.url = new_url
        except AttributeError:
            continue
            
    # Save changes
    wm.update()

 

- Josh Carlson
Kendall County GIS

View solution in original post

4 Replies
DavidPike
MVP Frequent Contributor

Hi, the following should list all your webmaps, layer titles and associated urls.  You can of-course put in some logic to search for particular strings, or just run it then ctrl-f to look for your particular layers.  This is using the arcgis api for Python.

from arcgis.gis import GIS
import arcgis

gis = GIS('https://www.arcgis.com', 'username', 'password')

webmaps = gis.content.search('*', item_type='Web Map')
for webmap in webmaps:
    wm_obj = arcgis.mapping.WebMap(webmap)
    print(webmap)
    print('layers:')
    for layer in wm_obj.layers:        
        print('\t' + layer.title, layer.url)
    print("________")
jcarlson
MVP Esteemed Contributor

For the find-and-replace process, you really just need the AGO Assistant.

jcarlson_0-1624280216362.png

Once you're connected to your portal, you can select Update the URLs of Services in a Web Map and you'll see the following interface once you've clicked on a map:

jcarlson_1-1624280418134.png

Notice that with the URL entered in Find, I see a little notice that one of the layers in the selected map matches the service URL. Clicking Replace and Update is all you need to do.

Coupled together with @DavidPike's script for finding the maps in question, you could easily go through and replace all the layers.

- Josh Carlson
Kendall County GIS
jcarlson
MVP Esteemed Contributor

One Fell Swoop

If you have a lot of maps to update, or you just trust your coding and don't require feedback and interaction for each change, this will update every map that has the layer you're replacing in one go.

from arcgis.gis import GIS
from arcgis.mapping import WebMap

gis = GIS('your-org-url', 'username', 'password')

# Don't forget to go all the way to the layer index, i.e., 'FeatureServer/0'!
old_url = 'url-to-your-old-layer'
new_url = 'url-to-your-new-layer'

# Find all maps in org
webmaps = gis.content.search('', item_type='Web Map', max_items=-1)

# Iterate over operational and basemap layers, updating URL
for w in webmaps:
    wm = WebMap(w)
    for b in wm.basemap.baseMapLayers:
        try:
            if b.url == old_url:
                b.url = new_url
        except AttributeError:
            continue
                
    for l in wm.layers:
        try:
            if l.url == old_url:
                l.url = new_url
        except AttributeError:
            continue
            
    # Save changes
    wm.update()

 

- Josh Carlson
Kendall County GIS
LindsayRaabe_FPCWA
Occasional Contributor III

Thanks @DavidPike and @jcarlson for your answers. All 3 were helpful and will be of use in the future. I hadn't seen the AGO Assistant before and my python is probably just beyond entry level so can read and rearrange but not really any good at writing from scratch. Cheers!

 

A couple of quick notes though - I've just test the ArcGIS Online Assistant to try the find/replace function and whilst the URL was indeed replaced, the layer information wasn't. When you look at it in the map, the name is the old name and the page opened when you click on the item properties is the old items page. It really is just a replacement of the underlying URL but nothing else. 

 

Also, I tried the find and print python script and whilst it sort of works, it only printed off about half a dozen maps and their layers - not the few dozen that I would expect to see. The maps printed were from an assortment of users in the org (including the admin account used to run the process) but defintely fell short of what I expected to see - and I can't see why. Haven't tried script 3 as I'm not confident (yet) that I want to try that option until I understand it a little better. 

Lindsay Raabe
GIS Officer
Forest Products Commission WA