Select to view content in your preferred language

Update an ArcGIS Online hosted feature layer using a Python Script

06-13-2023 09:33 PM
New Contributor

I'm using a Python script that updates an ArcGIS Online hosted feature layer.

The script runs OK but unselects "Enable Sync (required for offline use and collaboration)" and "Allow others to export to different formats".

I'd like the script to re-enable both settings during the update. Below is a copy of the current script. Any assistance would be appreciated.

import arcpy
import os, sys
from arcgis.gis import GIS

### Start setting variables

# Enter the path to the AGP Project that was used to publish the service to AGOL
prjPath = r"C:\GIS_Scripts\SmarterWX\SMWXAuto.aprx"

# Enter AGOL Service Definition Name
sd_fs_name = "DBYD"
portal = "" # Can also reference a local portal
# Enter credentials for the owner of the AGOL Service
user = "********"
password = "********"
# Set Sharing Options
shrOrg = True
shrEveryone = False
shrGroups = "DBYD"

### End settng variables

# Enter the path to the temporary output location
relPath = r'C:\GIS_Scripts\SmarterWX\SMWXAutoOut'
sddraft = os.path.join(relPath, "temporary service name.sddraft")
sd = os.path.join(relPath, "temporary service")

print("Creating SD file")
arcpy.env.overwriteOutput = True
prj =
mp = prj.listMaps()[0]

sharing_draft = mp.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", "DBYD")
sharing_draft.summary = "My Summary"
sharing_draft.tags = "My Tags"
sharing_draft.description = "My Description"
sharing_draft.credits = "My Credits"
sharing_draft.useLimitations = "My Use Limitations"

arcpy.StageService_server(sddraft, sd)

print("Connecting to {}".format(portal))
gis = GIS(portal, user, password)

# Find the SD, update it, publish /w overwrite and set sharing and metadata
print("Search for original SD on portal…")
print(f"Query: {sd_fs_name}")
sdItem =, item_type="Service Definition")
while sdItem[i].title != sd_fs_name:
i += 1
print('Item Found')
print(f'item[i].title = {sdItem[i].title}, sd_fs_name = {sd_fs_name}')
item = sdItem[i]

print("Overwriting existing feature service…")
fs = item.publish(overwrite=True)

if shrOrg or shrEveryone or shrGroups:
print("Setting sharing options…")
fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)

print("Finished updating: {} – ID: {}".format(fs.title,


Thank you

0 Kudos
2 Replies
by Anonymous User
Not applicable

It would help if you pasted your code with formatting:  click the '...', then '</>', paste in the code in the window and select python.

There is a section in the docs for enabling sync- featuresharingdraft-class and the export, it is a property in the service definition draft you can set.

sharing_draft.allowExporting = True


This statement is convoluted:

if shrOrg or shrEveryone or shrGroups:
    print("Setting sharing options…")
    fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)

and is really saying as set:

if True or False or 'DBYD':
    print("Setting sharing options…")
    fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)

 The conditional really isn't doing anything.

Check the example for sharing in the same featuresharingdraft docs- it uses an xml editor to edit the sddraft. Why it cant be an exposed property like the others when creating the sddraft is a mystery.

# Change following to "true" to share
SharetoOrganization = "false"
SharetoEveryone = "true"
SharetoGroup = "false"
# If SharetoGroup is set to "true", uncomment line below and provide group IDs
GroupID = ""    # GroupID = "f07fab920d71339cb7b1291e3059b7a8, e0fb8fff410b1d7bae1992700567f54a"

# Each key has a corresponding value. In all the cases, value of key_list[i] is value_list[i].
for i in range(key_list.length):
    if key_list[i].firstChild.nodeValue == "PackageUnderMyOrg":
        value_list[i].firstChild.nodeValue = SharetoOrganization
    if key_list[i].firstChild.nodeValue == "PackageIsPublic":
        value_list[i].firstChild.nodeValue = SharetoEveryone
    if key_list[i].firstChild.nodeValue == "PackageShareGroups":
        value_list[i].firstChild.nodeValue = SharetoGroup
    if SharetoGroup == "true" and key_list[i].firstChild.nodeValue == "PackageGroupIDs":
        value_list[i].firstChild.nodeValue = GroupID

# Write to the .sddraft file
f = open(sddraft_output_filename, 'w')


0 Kudos
New Contributor

Hi Jeff, thank you for your reply and advice. I'll have a closer look and let you know the result.

0 Kudos