How to publish a feature service to ArcGIS Online from an mxd USING PYTHON?

25894
45
Jump to solution
03-25-2013 07:06 PM
BenoitMetzger
Deactivated User
Hi All,

I would like to automate publishing feature services - from an mxd using Python to my AG Online account.

I did this successfully manually using the menu: Share as -> Service, Publish a service - Feature Access Capabilities etc...

Using Python, I managed to publish the service definition file on AG Online using:
arcpy.mapping.CreateMapSDDraft()
arcpy.mapping.AnalyzeForSD()
arcpy.StageService_server()
arcpy.UploadServiceDefinition_server()

But I could not find a way to publish a feature service to AG Online! I must be missing something. Any help would be really appreciated.

Thanks in advance!
Tags (2)
45 Replies
huaweiYin
Emerging Contributor
First, I want to give you guys a big Thank you to provide very rich-full information. I am publishing and updating my stuff on ARCGISONLINE perfectly with 10.1 every week.

However, I got some issues for 10.2. I sign in my account manually, and running the scripts, it gave me the error message below:

ExecuteError: ERROR 001566: Service overwrite error: failed to delete the service.
ERROR: code:400, Item '37a9c329cea54f339b56d142258e387d' does not exist or is inaccessible., Bad syntax in request.
Failed to execute (UploadServiceDefinition).

So i think in 10.2 it can not overwrite in ARCGIS ONLINE with the script in 10.1. But if I delete the files I published on AGO, then republish it, that works well, so just can not overwite.

Anyone has any idea how to solve this overwrite problem in 10.2?

Appropriate!

Howie
0 Kudos
MichaelVolz
Esteemed Contributor
Besides the overwrite issue, has anyone been able to get this automated process to work in v10.2?

Can you get this automated process to work on a brand new service where there is no overwrite to be done?
0 Kudos
KerstinAgatz
New Contributor
Hello,

is there a possibilty to turn on "enable attachments" for a feature in a feature service using  python code to publish a feature servide?

Thanks Kerstin
0 Kudos
KevinHibma
Esri Regular Contributor
Pretty sure this will enable attachments, it works for me. Update variables at the bottom.
You can switch TRUE to FALSE to disable attachments.

import json, urllib, urllib2

def gentoken(username, password, referer, expiration=60):
    #Re-usable function to get a token required for Admin changes
    referer = "http://www.arcgis.com/"
    
    query_dict = {'username': username,
                  'password': password,
                  'expiration': str(expiration),
                  'client': 'referer',
                  'referer': referer,
                  'f': 'json'}    
    
    query_string = urllib.urlencode(query_dict)
    tokenUrl = "https://www.arcgis.com/sharing/rest/generateToken"
    
    tokenResponse = urllib.urlopen(tokenUrl, urllib.urlencode(query_dict))   
    token = json.loads(tokenResponse.read())
        
    if "token" not in token:
        print token['messages']
        import sys
        sys.exit()
    else:
        # Return the token to the function which called for it
        return token['token']
    
def enableAttachment(token, fsURL):
    
    query_dict = {'updateDefinition': "{'hasAttachments':true}",   #false         
                  'f': 'json',
                  'token': token}    
    
    query_string = urllib.urlencode(query_dict)   
    
    attachResponse = urllib.urlopen(fsURL, urllib.urlencode(query_dict))   
    msg = json.loads(attachResponse.read())
    print msg 
    
    return


USERNAME = " ______  "
PASSWORD = " ______  "
REFFER = "http://www.arcgis.com"

fsURL = "http://services1.arcgis.com/<ORGANIZATION IDENTFIER/arcgis/admin/services/<SERVICE NAME>.FeatureServer/0/updateDefinition"
# for example, a link may look like:
#http://services1.arcgis.com/4safsf2dgg32daf3/arcgis/admin/services/MyService.FeatureServer/0/updateDefinition

token = gentoken(USERNAME, PASSWORD, REFFER)
enableAttachment(token, fsURL)
0 Kudos
KerstinAgatz
New Contributor
Thank you very much!

I tried a little bit and now it works!!!!:o


Best regards Kerstin
0 Kudos
huaweiYin
Emerging Contributor
Besides the overwrite issue, has anyone been able to get this automated process to work in v10.2?

Can you get this automated process to work on a brand new service where there is no overwrite to be done?


It works for publishing brand new service with the python script in 10.2, only I have to delete the old one, because the overwrite issue. btw, make sure you sign in AGO manually.

Howie
0 Kudos
huaweiYin
Emerging Contributor
One more issue for AGO is the "Max records return by server". It worked if publish files manually to change the number of the max return results, it default by 1000 btw, my record are way over 1000, I need to change the parameter every time when I publish. The python script works perfectly in my end on 10.1 without changing the max return results parameter, 10.2 had the overwrite issue, so I am going to keep using 10.1 to automate publish process.

I wonder Robert or anyone have any ideas to custom the max return result in python script as well?

Appreciated!

Howie
0 Kudos
by Anonymous User
Not applicable
You could probably script something through the server admin page.
http://forums.arcgis.com/threads/94744-Is-it-possible-to-change-the-Max-Record-Count-on-an-AGOL-host...

Please note the warning that you can damage your service as much as you can improve it.

Also, the limit is on features in a request, not the map. If editing is disabled in the Feature Service or web map, you will see multiple requests made. If 4 requests are made, and you have 2000 max record count, you could potentially have 8000 features returned.

Thanks,
Andrew
0 Kudos
AlexSanders
Frequent Contributor
Can you provide the script to change the enable editor tracking to true "enableEditorTracking" : true?

Thank you for any response.
Pretty sure this will enable attachments, it works for me. Update variables at the bottom.
You can switch TRUE to FALSE to disable attachments.

import json, urllib, urllib2

def gentoken(username, password, referer, expiration=60):
    #Re-usable function to get a token required for Admin changes
    referer = "http://www.arcgis.com/"
    
    query_dict = {'username': username,
                  'password': password,
                  'expiration': str(expiration),
                  'client': 'referer',
                  'referer': referer,
                  'f': 'json'}    
    
    query_string = urllib.urlencode(query_dict)
    tokenUrl = "https://www.arcgis.com/sharing/rest/generateToken"
    
    tokenResponse = urllib.urlopen(tokenUrl, urllib.urlencode(query_dict))   
    token = json.loads(tokenResponse.read())
        
    if "token" not in token:
        print token['messages']
        import sys
        sys.exit()
    else:
        # Return the token to the function which called for it
        return token['token']
    
def enableAttachment(token, fsURL):
    
    query_dict = {'updateDefinition': "{'hasAttachments':true}",   #false         
                  'f': 'json',
                  'token': token}    
    
    query_string = urllib.urlencode(query_dict)   
    
    attachResponse = urllib.urlopen(fsURL, urllib.urlencode(query_dict))   
    msg = json.loads(attachResponse.read())
    print msg 
    
    return


USERNAME = " ______  "
PASSWORD = " ______  "
REFFER = "http://www.arcgis.com"

fsURL = "http://services1.arcgis.com/<ORGANIZATION IDENTFIER/arcgis/admin/services/<SERVICE NAME>.FeatureServer/0/updateDefinition"
# for example, a link may look like:
#http://services1.arcgis.com/4safsf2dgg32daf3/arcgis/admin/services/MyService.FeatureServer/0/updateDefinition

token = gentoken(USERNAME, PASSWORD, REFFER)
enableAttachment(token, fsURL)
0 Kudos
KevinBingham
Emerging Contributor
Just for anyone else who comes upon this thread...
Jeff and I posted a blog with the python script to:
-CreateMapSDDraft, Analyze, Stage and Upload for map documents to be pushed to ArcGIS.com as a hosted feature service /w overwrite and shared to arcgis.com turned on.

http://blogs.esri.com/esri/arcgis/2013/04/23/updating-arcgis-com-hosted-feature-services-with-python...


Kevin and Jeff -  Thanks much for the blog post.

When attempting to run this python script I have encountered an error.  "ERROR 001269: Compressing the service definition failed.  Failed to execute (StageService)." (image attached)

Any ideas as to what is causing this error?

Thanks,

Kevin
0 Kudos