Python: automate field based on editor and edit date

2797
2
Jump to solution
07-14-2014 02:31 PM
New Contributor II

Hello all,

I am writing a script to automate a GIS data editing process:

I want two fields to be updated ("EDITOR" and "EDIT_DATE") every time a user edits a feature class within a geodatabase. I want the "EDITOR" field to be populated with the name of the user that is doing the editing and I want the "EDIT_DATE" field to be updated with date ("7/14/14") in which the editing was performed. Also, if the user has not created these fields ("EDITOR" and "EDIT_DATE"), I want the script to add the fields and update them accordingly.

Here is what I have so far (The script creates the "EDITOR field" but does not populate it with the username; The "EDIT_DATE" does the same but the date is wrong - it populates the field with rubbish):

I am not quite sure if I am on the right track here or not

# Developed by: David Bailey

# Contact: davidjonathanbailey@gmail.com

# Description: This tool updates the EDITOR and EDIT_DATE fields for all feature classes within a geodatabase

# based on the time the user makes the edits

# import arcpy site-package and os module

import arcpy, os, getpass, datetime

# Set geoprocessing environment:

# a) set the workspace environment

aWS = r"R:\Projects\1258_CLTP\Data\Aspen\Other_geodatabases\AV_Clearview.gdb"

arcpy.env.workspace = aWS

# Set geoprocessing environment:

# b) the overwriteOutput parameter controls whether tools will

#    automatically overwrite any existing output

arcpy.env.overwriteOutput = True

try:

    # Set Variables

    fcList = arcpy.ListFeatureClasses()

    Name_Expression = getpass.getuser()

    Date_Expression = datetime.datetime.date()

    #dropFields = ("EDIT_DATE", "EDITOR")

    # Iterate through feature classes - update EDITOR field

    for aFC in fcList:

        fieldList = arcpy.ListFields(aFC)

        for field in fieldList:  

            if field.name == "EDITOR":

                arcpy.CalculateField_management(aFC, "EDITOR", Name_Expression, "VB", "")

            else:

                arcpy.AddField_management(aFC, "EDITOR", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

                arcpy.CalculateField_management(aFC, "EDITOR", Name_Expression, "VB", "")

   

    # Iterate through feature classes - update EDIT_DATE field

    #for aFC in fcList:

        #fieldList = arcpy.ListFields(aFC)

        #for field in fieldList:  

            #if field.name == "EDIT_DATE":

               # arcpy.CalculateField_management(aFC, "EDIT_DATE", Date_Expression, "VB", "")

           # else:

             #   arcpy.AddField_management(aFC, "EDIT_DATE", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

               # arcpy.CalculateField_management(aFC, "EDIT_DATE", Date_Expression, "VB", "")

           

    # Delete Fields - this is only if you are working and editing the script - optional

    #for aFC in fcList:

        #arcpy.DeleteField_management(aFC, dropFields)

except:

   

    # Code to run when an error occured

    print "An ERROR Occured!"

    print "\n" + arcpy.GetMessages()

else:

   

    # Message when there was no error

    print "\nNo Error occurred"

   

# Script end message

print "\nEnd of the script!" 

I'd really appreciate some help - thanks!

Reply
0 Kudos
1 Solution

Accepted Solutions
Esri Contributor

David,

The functionality you are describing is available through editor tracking (no scripting necessary). Check out this link for more information: Editor Tracking

You can either enable it manually: Enable Editor Tracking

Or use GP tools to do this: using GP

In your case you could just enable Editor and Edit Date, you don't need to enable everything. The bonus of doing this vs a script is that it will also work in ArcGIS Server and if you are in an enterprise geodatabase it will not cause conflicts.

Hope this helps,

Russell

View solution in original post

Reply
0 Kudos
2 Replies
Esri Contributor

David,

The functionality you are describing is available through editor tracking (no scripting necessary). Check out this link for more information: Editor Tracking

You can either enable it manually: Enable Editor Tracking

Or use GP tools to do this: using GP

In your case you could just enable Editor and Edit Date, you don't need to enable everything. The bonus of doing this vs a script is that it will also work in ArcGIS Server and if you are in an enterprise geodatabase it will not cause conflicts.

Hope this helps,

Russell

View solution in original post

Reply
0 Kudos
New Contributor II

Russell,

Thanks! We don't have an ArcGIS server and I thought that tool was only for an Enterprise system.

Thanks for the help!