<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic arcpy.AlterAliasName doesn't apply to dataset until map is closed in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/arcpy-alteraliasname-doesn-t-apply-to-dataset/m-p/1173190#M55078</link>
    <description>&lt;P&gt;Short version:&lt;/P&gt;&lt;P&gt;arcpy.AlterAliasName immediately updates alias of a feature class dataset when run from python window. Yet when run as script tool, changes aren't applied until the map is exited.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Background and context:&lt;/P&gt;&lt;P&gt;I have an ArcGIS Pro Desktop project with 230 layers in its table of contents (TOC). Many are geodatabase feature classes which I have given user-friendly names to in the TOC.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can use a loop of arcpy.AlterAliasName to go through all layers in the map and update the data source alias of all geodatabase feature classes:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Code to loop through layers in a given map and update the data source alias with the layer name
#
#
aprx = arcpy.mp.ArcGISProject("CURRENT")
# !! Enter the name of the map you want to update aliases for
mapname = "Map"
m = aprx.listMaps(mapname)[0]
for l in m.listLayers():
    lname = l.name
    try:
        dsource = l.dataSource
        print("Writing layer name &amp;lt;" + lname + "&amp;gt; to dataset &amp;lt;" + dsource + "&amp;gt;...")
        # !!!KEY LINE OF SCRIPT!!!
        arcpy.AlterAliasName(dsource, lname)
        print("Successfully processed: &amp;lt;" + lname + "&amp;gt;")
    except Exception:
        print("Can't process: &amp;lt;" + lname + "&amp;gt;..group layer, non-gdb or read only?")
# END&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works perfectly when I paste it in the ArcGIS Pro Python window and run it. As soon as it is run, if I look at the feature class properties in Catalogue, the alias is updated with the description from the TOC.&lt;/P&gt;&lt;P&gt;My issue is with turning this into an ArcGIS Pro Script tool. When run as a script tool, the data source alias does not update until the map is closed in ArcGIS Pro.&amp;nbsp;&lt;/P&gt;&lt;P&gt;My script tool was just passing a single parameter to the script - a Map, selected by the user.&lt;/P&gt;&lt;P&gt;I thought I had cracked the problem when I came across the &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/geoprocessing_and_python/setting-script-tool-parameters.htm" target="_blank" rel="noopener"&gt;following&lt;/A&gt;, but when I added the second derived parameter, it made no difference.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Dirk_0-1652341735906.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/41195i7BD2395756EDEF36/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Dirk_0-1652341735906.png" alt="Dirk_0-1652341735906.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I'd really like to understand why arcpy.AlterAliasName works fine when run from the Python window, but when run within a script tool, won't apply changes to the dataset while the map is open.&lt;/P&gt;&lt;P&gt;It wouldn't be such an issue if I could automate closing of the map with the script tool/python code, but I don't think that's possible either?&lt;/P&gt;&lt;P&gt;FWIW, here is the code called by the script tool:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy


# Get the current project as an object
aprx = arcpy.mp.ArcGISProject("CURRENT")
# Get the selected map parameter as text from the script tool dialogue
mapName = arcpy.GetParameterAsText(0)
# Look up the correct map object from the list of maps in the project
m = aprx.listMaps(mapName)[0]

for l in m.listLayers():
    lname = l.name
    try:
        dsource = l.dataSource
        arcpy.AddMessage("Writing layer name &amp;lt;" + lname + "&amp;gt; to dataset &amp;lt;" + dsource + "&amp;gt;...")
        # Key line of script. Updates the alias.
        arcpy.AlterAliasName(dsource, lname)
        arcpy.AddMessage("Successfully processed: &amp;lt;" + lname + "&amp;gt;")
    except Exception:
        arcpy.AddMessage("Can't process: &amp;lt;" + lname + "&amp;gt;..group layer, non-gdb or read only?")

# Set a derived output parameter to get ArcGIS Pro to refresh the map and apply the changes
# Without this line the new aliases won't apply on the feature classes until map is closed
# Currently this doesn't work though!!
arcpy.SetParameterAsText(1,mapName)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 16 May 2022 01:07:19 GMT</pubDate>
    <dc:creator>Dirk</dc:creator>
    <dc:date>2022-05-16T01:07:19Z</dc:date>
    <item>
      <title>arcpy.AlterAliasName doesn't apply to dataset until map is closed</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/arcpy-alteraliasname-doesn-t-apply-to-dataset/m-p/1173190#M55078</link>
      <description>&lt;P&gt;Short version:&lt;/P&gt;&lt;P&gt;arcpy.AlterAliasName immediately updates alias of a feature class dataset when run from python window. Yet when run as script tool, changes aren't applied until the map is exited.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Background and context:&lt;/P&gt;&lt;P&gt;I have an ArcGIS Pro Desktop project with 230 layers in its table of contents (TOC). Many are geodatabase feature classes which I have given user-friendly names to in the TOC.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can use a loop of arcpy.AlterAliasName to go through all layers in the map and update the data source alias of all geodatabase feature classes:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Code to loop through layers in a given map and update the data source alias with the layer name
#
#
aprx = arcpy.mp.ArcGISProject("CURRENT")
# !! Enter the name of the map you want to update aliases for
mapname = "Map"
m = aprx.listMaps(mapname)[0]
for l in m.listLayers():
    lname = l.name
    try:
        dsource = l.dataSource
        print("Writing layer name &amp;lt;" + lname + "&amp;gt; to dataset &amp;lt;" + dsource + "&amp;gt;...")
        # !!!KEY LINE OF SCRIPT!!!
        arcpy.AlterAliasName(dsource, lname)
        print("Successfully processed: &amp;lt;" + lname + "&amp;gt;")
    except Exception:
        print("Can't process: &amp;lt;" + lname + "&amp;gt;..group layer, non-gdb or read only?")
# END&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works perfectly when I paste it in the ArcGIS Pro Python window and run it. As soon as it is run, if I look at the feature class properties in Catalogue, the alias is updated with the description from the TOC.&lt;/P&gt;&lt;P&gt;My issue is with turning this into an ArcGIS Pro Script tool. When run as a script tool, the data source alias does not update until the map is closed in ArcGIS Pro.&amp;nbsp;&lt;/P&gt;&lt;P&gt;My script tool was just passing a single parameter to the script - a Map, selected by the user.&lt;/P&gt;&lt;P&gt;I thought I had cracked the problem when I came across the &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/geoprocessing_and_python/setting-script-tool-parameters.htm" target="_blank" rel="noopener"&gt;following&lt;/A&gt;, but when I added the second derived parameter, it made no difference.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Dirk_0-1652341735906.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/41195i7BD2395756EDEF36/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Dirk_0-1652341735906.png" alt="Dirk_0-1652341735906.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I'd really like to understand why arcpy.AlterAliasName works fine when run from the Python window, but when run within a script tool, won't apply changes to the dataset while the map is open.&lt;/P&gt;&lt;P&gt;It wouldn't be such an issue if I could automate closing of the map with the script tool/python code, but I don't think that's possible either?&lt;/P&gt;&lt;P&gt;FWIW, here is the code called by the script tool:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy


# Get the current project as an object
aprx = arcpy.mp.ArcGISProject("CURRENT")
# Get the selected map parameter as text from the script tool dialogue
mapName = arcpy.GetParameterAsText(0)
# Look up the correct map object from the list of maps in the project
m = aprx.listMaps(mapName)[0]

for l in m.listLayers():
    lname = l.name
    try:
        dsource = l.dataSource
        arcpy.AddMessage("Writing layer name &amp;lt;" + lname + "&amp;gt; to dataset &amp;lt;" + dsource + "&amp;gt;...")
        # Key line of script. Updates the alias.
        arcpy.AlterAliasName(dsource, lname)
        arcpy.AddMessage("Successfully processed: &amp;lt;" + lname + "&amp;gt;")
    except Exception:
        arcpy.AddMessage("Can't process: &amp;lt;" + lname + "&amp;gt;..group layer, non-gdb or read only?")

# Set a derived output parameter to get ArcGIS Pro to refresh the map and apply the changes
# Without this line the new aliases won't apply on the feature classes until map is closed
# Currently this doesn't work though!!
arcpy.SetParameterAsText(1,mapName)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 May 2022 01:07:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/arcpy-alteraliasname-doesn-t-apply-to-dataset/m-p/1173190#M55078</guid>
      <dc:creator>Dirk</dc:creator>
      <dc:date>2022-05-16T01:07:19Z</dc:date>
    </item>
  </channel>
</rss>

