I have been trying to figure out how I can set a default value to a field in ArcMap. I see there is a tool called Assign Default to field but that is applied to the entire dataset. I would like the default to apply to the current ArcMap session only. In the Create Features window you can right click a layer and set a default there to get the behavior I would like but am trying to accomplish this for multiple layers in the TOC using python.
This is what I have been trying, starting with one layer first, but get an error "The attribute 'defaultValue' is not supported on this instance of Field" I can print the defaultValue however.
feature_class = "Roads"
fields = arcpy.ListFields(feature_class)
for field in fields:
if field.name == "Project_Name":
field.defaultValue = "Test_Project"
Forgive me if I am making an obvious mistake. I am still a beginner. Thanks for any help!
import os
import arcpy
def AssignDefault(tbl, fld):
try:
fname = arcpy.ListFields(tbl, fld)[0].name
mxd = arcpy.mapping.MapDocument("CURRENT")
default = os.path.splitext(arcpy.Describe(mxd.filePath).name)[0]
arcpy.AssignDefaultToField_management(tbl, fname, default)
print default
except:
print("Could not assign default")
AssignDefault("Roads", "Project_Name")
Thanks Dan. I haven't used functions much. I did get it to run but as I thought, the default value was assigned to the feature class itself so when I go to another map the same default value is present. That is why I was trying to use the defaultValue property for the field. What I really need is a default on a field that will exist for in the mxd only. Just like how you can set it in the template properties when creating a new feature. Maybe this is not possible.
No, it isn't possible because that default value is a property of the data, not an mxd which points to it.
Have you looked at the Attribute Assistant? It allows you to establish a default value for a field.
You need to have a table called DynamicValue added to your map.
The Attribute Assistant has a method called EXPRESSION that would allow you to put a value in a field.
I actually just tested this on a feature class called Roads with a text field of Project_Name. The entry in the DynamicValue table looks like this
I don't think there's a place you can download the Attribute Assistant by itself. It comes packaged with the Local Government map templates in the maps and apps folder that comes with the bundle. You can download the Address Management template here, open up the template and look at their DynamicValue table to see how it is configured.
Thanks Jeff. I will look at that. Thanks to all others who tried to help me! Just to be sure,before I give up, there is no way to access the Create Feature Template Properties with python and I cannot use field.defaultValue to assign a default? Many thanks!
Hi Stacy, did you ever find a solution to this? I'm trying to do something very similar. Seems like you'd have to access the edit features template to change the default value only in the map document. I haven't found a way to do that in ArcPy.
Hi Nils. Sorry it took me so long to answer you. There is no way to do this with ArcPy. I had to learn some ArcObjects to accomplish this. You can set a default value through the layer extensions (ILayerExtension) and then access the template manager (IEditTemplateManager) and edit the template (IEditTemplate). One problem I ran into that is now logged as a bug is that you have to start an edit session for this to work. If you would like some sample code let me know.
Hi Stacy, we're looking at a very similar job to your original post. Could you please post your sample code? If it's too long Pastebin.com would be fine.
Hi Andrew. Just seeing this now. Here it is. I was hoping to do this outside of ArcMap but found that you have to start an editing session to get it to work so created it as an Add-In. I have reported this to ESRI as a bug but don't think it has been fixed yet.
Protected Overrides Sub OnClick()
Dim ipDoc As ESRI.ArcGIS.ArcMapUI.IMxDocument 'ArcMapUI
ipDoc = CType(My.ArcMap.Application.Document, ESRI.ArcGIS.ArcMapUI.IMxDocument)
Dim pMap As ESRI.ArcGIS.Carto.IMap
pMap = ipDoc.FocusMap
Dim pDocInfo As IDocumentInfo2 = CType(ipDoc, IDocumentInfo2)
Dim pEnumLayer As IEnumLayer
Dim pLayer As ILayer
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureClass As IFeatureClass
Dim pDataset As IDataset
Dim Plan_Num As String
Dim idx As Integer
Dim m_editor As IEditor
Dim editorUid As New UID()
editorUid.Value = "esriEditor.Editor"
m_editor = TryCast(My.ArcMap.Application.FindExtensionByCLSID(editorUid), IEditor)
'Gets numbers from the name of the map which is the Plan_Num and will be used to set default values
Plan_Num = Integer.Parse(Regex.Replace(pDocInfo.Name, "[^\d]", ""))
pEnumLayer = pMap.Layers(Nothing, True)
pEnumLayer.Reset()
pLayer = pEnumLayer.Next
For idx = 0 To pMap.LayerCount - 1
Do Until pLayer Is Nothing
If TypeOf pLayer Is IFeatureLayer2 And Not TypeOf pLayer Is RasterLayer Then
Try
pFeatureLayer = pLayer
pFeatureClass = pFeatureLayer.FeatureClass
If Not pFeatureClass.FeatureDataset Is Nothing Then
pDataset = pFeatureClass.FeatureDataset
'Loop through layers in the Plans feature dataset and set default value for Plan_Num
If pDataset.Name = "REF.SDE.Plans" Then
Dim pWorkSpace As IWorkspace = pDataset.Workspace
Dim pWorkEdit As IWorkspaceEdit = pWorkSpace
If pWorkEdit.IsBeingEdited = False Then
'Start an edit session
m_editor.StartEditing(pDataset.Workspace)
pFeatureLayer = pLayer
Dim pLayerExt As ESRI.ArcGIS.Carto.ILayerExtensions
pLayerExt = CType(pFeatureLayer, ESRI.ArcGIS.Carto.ILayerExtensions)
Dim jdx As Integer
For jdx = 0 To pLayerExt.ExtensionCount - 1
Dim obj As Object = pLayerExt.Extension(jdx)
If TypeOf obj Is ESRI.ArcGIS.Carto.IEditTemplateManager Then
Dim pEditTemplateMgr As ESRI.ArcGIS.Carto.IEditTemplateManager
pEditTemplateMgr = CType(obj, ESRI.ArcGIS.Carto.IEditTemplateManager)
Dim pEditTemplate As IEditTemplate = pEditTemplateMgr.EditTemplate(jdx)
Dim kdx As Integer
For kdx = 0 To pEditTemplateMgr.Count - 1
pEditTemplateMgr.EditTemplate(kdx).SetDefaultValue("Plan_num", Plan_Num, True)
Next
End If
Next
m_editor.StopEditing(False)
Else
MsgBox("Please stop editing and try to run tool again")
End If
End If
End If
Catch ex As Exception
MsgBox("Error! Default value not set for " + pLayer.Name)
End Try
End If
pLayer = pEnumLayer.Next
Loop
Next
My.ArcMap.Application.SaveDocument()
MsgBox("Process complete. Map can be closed.")
My.ArcMap.Application.CurrentTool = Nothing
End Sub