Is there a way to automatically change an MXD data source to a hosted feature service on AGO?

2420
3
10-27-2015 10:36 AM
BrianFausel
Occasional Contributor III

I have found some very useful solutions to change MXD data sources using python.  The question: Is it possible to automatically switch MXD data sources to an AGO-hosted feature service?  It seems that the replaceDataSource() method would work, but there does not appear to be a workspace_type for AGO.

The added challenge here is that the feature class names change - the original file GDB uses the full name (i.e. "Major_Employers") and on AGO the name becomes the number for the REST service (i.e. "0").

I tried modifying the python example "A feature class is renamed" from the Esri help document and it didn't work.  Before I move forward with troubleshooting this code, my main question is can this be done in the first place?

Note: I modified the AGO URL in my code example below to remove specifics:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\temp\MXD_Update\Data_Source_Update.mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DATASOURCE"):
        if lyr.dataSource == r"C:\temp\MXD_Update\AGO_Sync_Testing.gdb\Major_Employers":
            lyr.replaceDataSource(r"http://services2.arcgis.com/[url]/ArcGIS/rest/services/[featureservicename]/FeatureServer", "NONE", "0")
            lyr.name = "0"
mxd.saveACopy(r"C:\temp\MXD_Update\Data_Source_Update_AGO.mxd")
del mxd
0 Kudos
3 Replies
RebeccaStrauch__GISP
MVP Emeritus

Brian, I have an addin that lets you replace ArcGIS Server service URL's....I wonder if it is a similar process?

Python addin for data inventory and “broken-link” repair.

I have not tried replacing, but looking at the code in the addin might give you some clues.

0 Kudos
BrianFausel
Occasional Contributor III

Thanks Rebecca, I will take a look at the addin soon.  If it doesn't do the trick for AGO, at the very least the addin looks helpful for other data source issues.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I'm have a newer version, but not ready to post (no time to work on it right now). But if you find it helpful, you can follow the blog page I think.

If you find the code just needs a little tweak of some type to get it to work with AGOL, let me know.  It would be good to add that in.  If it can't be done at all, it sounds like somehting that should be added on the Esri Arcgis Ideas | Ideas Submission Portal page

0 Kudos