I think this is what you are trying to achieve based on what you posted. I switched to using os.path to parse your filename and using an arcpy.da cursor using a with statement instead of the cursor you were using (and not cleaning up appropriately at the end, which might have kept a handle to the old PDS Temp active). You also do not need to iterate over field names, just give it the one field name that you are changing since you change it for every row anyway. I added in a test for the presence of the field name, just in case.
I suspect that your cursor not being closed was doing something weird with the PDS Temp reference, so that you are somehow still referencing the PDS Temp in the last map document. Not sure though, since I do not know the behavior of Add Ins very well.
The code sample below is untested (I don't have the python add-in wizard installed). As I noted with my comment, your one print statement should print a whole bunch. I have no idea where print statements in add-ins output to. One other thing to consider is defining self.desc in __init__ and then clearing out self.desc at the end of onClick so that desc is not reinitialized with every click.
Note: Change ButtonClass to be the appropriate class name for your button that you got from the python add-in wizard.
import arcpy
from os.path import basename
from os.path import splitext
class ButtonClass(object):
def __init__(self):
self.enabled = True
self.checked = False
self.mxd = arcpy.mapping.MapDocument('current')
def onClick(self):
desc = arcpy.Describe("PDS Temp")
if desc.FIDSet:
lyrs = arcpy.mapping.ListLayers(self.mxd, '', arcpy.mapping.ListDataFrames(self.mxd)[0])
fileName = splitext(basename(self.mxd.filePath))[0]
for layer in lyrs:
if layer.longName == 'PDS Temp' and len(arcpy.ListFields(layer, 'Search_Reference')):
edit = arcpy.da.Editor(layer.workspacePath)
edit.startEditing(False, False) #Assumes unversioned data
edit.startOperation()
try:
with arcpy.da.UpdateCursor(layer, ['Search_Reference']) as cursor:
for row in cursor:
row[0] = fileName
cursor.updateRow(row)
#The next line will print for every row you update!
print('Search_Reference updated successfully.')
except:
raise
finally:
edit.stopOperation()
edit.stopEditing(True)
else:
print('Nothing selected.')
EDIT: An alternate way to do this, with the button being enabled and disabled based on there being an edit selection available.
import arcpy
import pythonaddins
from os.path import basename
from os.path import splitext
class ButtonClass(object):
def __init__(self):
self.enabled = False
self.checked = False
self.mxd = arcpy.mapping.MapDocument('current')
def onClick(self):
fileName = splitext(basename(self.mxd.filePath))[0]
lyrs = arcpy.mapping.ListLayers(self.mxd, '', arcpy.mapping.ListDataFrames(self.mxd)[0])
for layer in lyrs:
if layer.longName == 'PDS Temp' and len(arcpy.ListFields(layer, 'Search_Reference')):
arcpy.CalculateField_management(layer, 'Search_Reference', '"{0}"'.format(fileName, "PYTHON")
else:
print('Nothing selected.')
class ExtensionClass(object):
def __init__(self):
self.enabled = True
def enableCheck(self):
# tool1 is the tool ID (without the namespace prefix) for your button add-in
# Should probably use self.currentLayer as well to make sure you have the right layer
if len(self.editSelection) == 0:
tool1.enabled = False
else:
tool1.enabled = True
arcpy.RefreshActiveView()
return
def onStartEditing(self):
self.enableCheck(self)
return
def onStopEditing(self):
# tool1 is the tool ID (without the namespace prefix) for your button add-in
tool1.enabled = False
arcpy.RefreshActiveView()
return
def onEditorSelectionChanged(self):
self.enableCheck(self)
return
descvariable is having issues and possibly your mxd variable. – blord-castillo Dec 03 '15 at 14:11desc, it still ends up referencing the old 'PDS Temp' instead of the new 'PDS Temp' – blord-castillo Dec 03 '15 at 14:23onClickfunction ? I would actually move the mxd declaration to the init function and useself.mxdinstead ofmxd. From some quick tests, if you use keyword "current" it always references the current document even across loads. Don't deletemxdat the end of your function. If this is wrapped in theonClickfunction, then you should not need to do any deleting of variables at the end. – blord-castillo Dec 03 '15 at 14:37