Select to view content in your preferred language

Update service URLs in Web Map

6000
6
Jump to solution
01-16-2020 05:20 PM
JosephRoberts2
Emerging Contributor

Due to a server migration, I need to update all the service URLs to the new server.  The script runs, however, I do not see the update for the layers in the web map. 

I took a look at this post and attempted to implement but still no update.  

Could someone please assist.  Posted code below

import arcgis.gis
from arcgis.mapping import WebMap
import os


if __name__ == "__main__":
    
    
    
    gis =arcgis.gis.GIS(orgportalURL,UN,Passowrd)
    user = gis.users.get(UN)
    print ("Successfully Logged in as " + user['username'])

    
    
    webMapID = "036292a6eb7b4bdFakeWEBMAP"
    print ("Gaining attributes for web map: " + str(webMapID))
    Webmap = gis.content.get(str(webMapID))
    wm = WebMap(Webmap)

    

    loop = 0
    for layer in wm.layers:
        upd = layer

        gisenturl = 'https://fakeportalname/fakeserver/rest/services/Environmental'
        gisviewurl = 'https://anotherfakeportalname/anotherfakeserver/rest/services/Environmental'
        if gisenturl in layer['url']:
            wm.layers[loop]['url'= layer['url'].replace(gisenturl,gisviewurl)
            gis.update_properties(wm.layers[0])
            print ("Service Updated for " + layer['title'])
        
        loop += 1
 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
AnttiKajanus3
Occasional Contributor

Does it help if you save the webmap with using update directly. I just answered to similar case where layer level properties are added and saved to the webmap (with different property on the layer though) in https://community.esri.com/thread/246654-setting-the-refresh-interval-for-a-hosted-feature-layer#com... 

webmap.update()

View solution in original post

6 Replies
AnttiKajanus3
Occasional Contributor

Does it help if you save the webmap with using update directly. I just answered to similar case where layer level properties are added and saved to the webmap (with different property on the layer though) in https://community.esri.com/thread/246654-setting-the-refresh-interval-for-a-hosted-feature-layer#com... 

webmap.update()
JosephRoberts2
Emerging Contributor

Thanks Antii. 

I added the wm.update() which essentially saved the webmap, which updated the service URLs. 

0 Kudos
MichaelVolz
Esteemed Contributor

Thanks for providing the code snippet.  Is there any chance you could migrate the server, yet utilize the same urls using aliases and/or the web adaptor for routing web traffic with the same url from a different server?

0 Kudos
AnttiKajanus3
Occasional Contributor

That should be ok. I'm no means on expert on that topic but you could start from this page.

You need to determine the URL format used by your existing apps when connecting to ArcGIS Server. This will help you understand whether your apps need to be modified after the upgrade. Using ArcGIS Web Adaptor, you can engineer your site to match the URLs you used at earlier versions, thereby saving yourself the time and effort of updating all your app code.

JakeSkinner
Esri Esteemed Contributor

Hi Joseph,

If you added the layers to your web map from items in your Portal, you may want to consider updating the item itself rather than just the layer in the web map:

Note:  Hosted feature service URLs cannot be updated

from arcgis.gis import GIS

# Variables
admin = 'portaladmin'
password = '*****'
portal = 'https://gis.esri.com/portal'
gisenturl = 'https://gis-stagin.esri.com/arcgis'
gisviewurl = 'https://gis.esri.com/arcgis'
targetUser = 'jskinner'

gis = GIS(portal, admin, password, verify_cert=False)

users = gis.users.search(targetUser)
for user in users:
    content = user.items()
    for layer in content:
        if 'hosted' not in layer.url:
            if gisenturl in layer.url:
               updateURL = layer.url.replace(gisenturl, gisviewurl)
               layer.update(item_properties={'url':updateURL})
               print("Updated {0}".format(layer.title))
    folders = user.folders
    for folder in folders:
        content = user.items(folder=folder['title'])
        for layer in content:
            if gisenturl in layer.url:
               updateURL = layer.url.replace(gisenturl, gisviewurl)
               layer.update(item_properties={'url':updateURL})
               print("Updated {0}".format(layer.title))
JamesTurner2
Frequent Contributor

Can you explain the advantage of this route? We just migrated from one hosting server to two (added a dedicated map image server). Our system admins cloned everything so now I have two of every service in portal; one on the old hosting server and one on the new map server. These are all reference registered feature services and the database has not moved. I'm not sure why this approach was taken but now I need to repoint all the data in my web maps to the services on the new server. I was thinking that I might be able to use the API to do this? Looping through all of my content to build an old server:new server dict and then looping through my maps, and using the dict to replace the old server URLs. Most maps have a high level of customization for symbology, pop ups, etc. so I really don't want to start over from scratch. Another thought I had was editing the JSON to update URL and itemID for the operational layers, but that seems less certain and more sketchy. Any advice is appreciated. 

0 Kudos