HOW TO

Publish a feature layer without overwriting the editor tracking information using ArcGIS API for Python

Last Published: November 28, 2022

Summary

When publishing a feature layer from ArcGIS Pro to ArcGIS Online or Portal for ArcGIS, it is possible to enable or disable the existing editor information. However, in ArcGIS Pro 2.5 and below, when publishing from ArcGIS Pro with existing editor tracking fields, the information is overwritten and replaced with the publisher's information and the date or time the layer is published. This is by design. However, it is possible to preserve the pre-existing editor tracking information using ArcGIS API for Python.

Follow the instructions below to publish a feature layer without overwriting the editor tracking information using ArcGIS API for Python.

Procedure

Note:
Publish the desired feature service from ArcGIS Pro before proceeding with these steps.

Download OverwriteExistingFeatureLayer.zip and extract the OverwriteExistingFeatureLayer.txt file. Insert the script to overwrite the existing feature layer and retain the original values of the editor tracking information:

  1. Import the necessary modules.
import arcpy
import os, sys
from arcgis.gis import GIS
  1. Specify the path to the project and the login credentials.
prjPath = r'C:\Users\pathwayToYour.aprx'

sd_fs_name = 'name of item in the portal'
portal = 'https://PortalURL/portal/home' #or https://arcgis.com
user = 'username'
password = 'password'
  1. Set the sharing options, a local path in the machine to store the temporary content, and create a new Service Definition Draft (.sddraft) file and stage to Service Definition (SD).
shrOrg = True
shrEveryone = False
shrGroups = ''

relPath = sys.path[0]
sddraft = os.path.join(relPath, 'WebUpdate.sddraft')
sd = os.path.join(relPath, 'WebUpdate.sd')

print('Creating SD file')
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]
arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, sd_fs_name, "MY_HOSTED_SERVICES", "FEATURE_ACCESS", None, True, True, True)
arcpy.StageService_server(sddraft, sd)
  1. Connect to the specified portal.
print('Connecting to {}'.format(portal))
gis = GIS(portal, user, password)
#if single sign on, edit line above be gis = GIS(portal)
  1. Find the SD file, update and overwrite the feature layer edits, including the snippet to preserve the timestamps.
print('Search for original SD on portal…')
sdItem = gis.content.search('{} AND owner:{}'.format(sd_fs_name, user), item_type='Service Definition')[0]
print('Found SD: {}, ID: {} n Uploading and overwriting…'.format(sdItem.title, sdItem.id))
pub_params = {"editorTrackingInfo" : {"enableEditorTracking":'true', "preserveEditUsersAndTimestamps":'true'}}
sdItem.update(data=sd)
print('Overwriting existing feature service…')
fs = sdItem.publish(publish_parameters=pub_params,overwrite=True)
fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)
print('Finished updating: {} – ID: {}'.format(fs.title, fs.id))

Article ID:000021839

Software:
  • ArcGIS Online
  • Portal for ArcGIS
  • ArcGIS API for Python

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic