<?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 Add a table to TOC through script (10.0) in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/add-a-table-to-toc-through-script-10-0/m-p/433685#M34064</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm trying to add a table to the table of contents from a arcpy script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It works fine through the command dialog but when using the commands within a script nothing seems to happen.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The command "arcpy.MakeTableView_management(...)" works fine in the command line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I can add a layer through the script using lines, but there's no option to add a table the same way.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd = arcpy.mapping.MapDocument("CURRENT")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;theShape = r"C:\Test.gdb\Test_FC"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;newlayer = arcpy.mapping.Layer(theShape)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.mapping.AddLayer(df, newlayer, "BOTTOM")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;del mxd, df, newlayer&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas or adive would be greatly appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Elliott&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 11 Feb 2014 20:37:44 GMT</pubDate>
    <dc:creator>ElliottCarson</dc:creator>
    <dc:date>2014-02-11T20:37:44Z</dc:date>
    <item>
      <title>Add a table to TOC through script (10.0)</title>
      <link>https://community.esri.com/t5/python-questions/add-a-table-to-toc-through-script-10-0/m-p/433685#M34064</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm trying to add a table to the table of contents from a arcpy script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It works fine through the command dialog but when using the commands within a script nothing seems to happen.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The command "arcpy.MakeTableView_management(...)" works fine in the command line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I can add a layer through the script using lines, but there's no option to add a table the same way.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd = arcpy.mapping.MapDocument("CURRENT")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;theShape = r"C:\Test.gdb\Test_FC"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;newlayer = arcpy.mapping.Layer(theShape)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.mapping.AddLayer(df, newlayer, "BOTTOM")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;del mxd, df, newlayer&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas or adive would be greatly appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Elliott&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Feb 2014 20:37:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-table-to-toc-through-script-10-0/m-p/433685#M34064</guid>
      <dc:creator>ElliottCarson</dc:creator>
      <dc:date>2014-02-11T20:37:44Z</dc:date>
    </item>
    <item>
      <title>Re: Add a table to TOC through script (10.0)</title>
      <link>https://community.esri.com/t5/python-questions/add-a-table-to-toc-through-script-10-0/m-p/433686#M34065</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;So is 'Test_FC' a Table or a Feature Class? &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You indicate Table:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;I'm trying to add a table to the table of contents from a arcpy script.&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;But FC indicates it might be a Feature Class...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If it is actually a GDB Table, you should be using:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd)[0]
TableView = arcpy.mapping.TableView(r'C:\Documents and Settings\jdk588\My Documents\ArcGIS\Default.gdb\MyTable')
arcpy.mapping.AddTableView(df, TableView)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You'll then need to manually switch the Table of Contents from the default 'List Layers by Drawing Order' view to the 'List By Source' view which is the only view under which you can see the table in the Table of Contents window.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I had hoped the 'arcpy.RefreshTOC()' function would automatically change the view based on the dataType of the last item added, but I found it doesn't and I don't know of any method in arcpy to change the TOC view, so you're probably stuck with the manual change now. Though, you could add a messagebox from the pythonaddins module to let you know the table was added.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd)[0]
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; TableView = arcpy.mapping.TableView(r'C:\Documents and Settings\jdk588\My Documents\ArcGIS\Default.gdb\MyTable')
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddTableView(df, TableView)
&amp;nbsp;&amp;nbsp;&amp;nbsp; pythonaddins.MessageBox("Table Added to TOC. Switch TOC to 'List By Source' view to access Table.", "Table Added", 0)
except Exception as e:
&amp;nbsp;&amp;nbsp;&amp;nbsp; pythonaddins.Messagebox("An error occured: " + e.message, "ERROR", 0)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:27:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-table-to-toc-through-script-10-0/m-p/433686#M34065</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2021-12-11T19:27:32Z</dc:date>
    </item>
    <item>
      <title>Re: Add a table to TOC through script (10.0)</title>
      <link>https://community.esri.com/t5/python-questions/add-a-table-to-toc-through-script-10-0/m-p/433687#M34066</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the reply John,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Unfortunately the commands you suggested are not available in 10.0.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I did end up finding the solution though. The script below requires an output to be defined in the parameters (see attachment).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;tbl = arcpy.CreateTable_management("in_memory", "table")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.AddField_management(tbl, "F1", "TEXT", field_length=14)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.AddField_management(tbl, "F2", "TEXT", field_length=25)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.AddField_management(tbl, "F3", "TEXT", field_length=25)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.AddField_management(tbl, "F4", "TEXT", field_length=25)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.SetParameterAsText(0,arcpy.MakeTableView_management(table, "view"))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.RefreshActiveView()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.RefreshTOC()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;del tbl&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Elliott&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Feb 2014 13:19:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-table-to-toc-through-script-10-0/m-p/433687#M34066</guid>
      <dc:creator>ElliottCarson</dc:creator>
      <dc:date>2014-02-12T13:19:56Z</dc:date>
    </item>
    <item>
      <title>Re: Add a table to TOC through script (10.0)</title>
      <link>https://community.esri.com/t5/python-questions/add-a-table-to-toc-through-script-10-0/m-p/433688#M34067</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;Unfortunately the commands you suggested are not available in 10.0.&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sorry Elliot, I didn't notice the 10.0 in parenthesis in your title. In that case, I would just use the MakeQueryTable tool, but omit any type of query. Kind of a workaround but a pretty elegant solution that could even be condensed to a single line if you just insert the path directly into the function instead of referring to a variable like I did. You don't even need to specify the MXD or DF with this tool and it will automatically switch your TOC view over to 'List by Source' once the Table is added.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
theTable = r'C:\Documents and Settings\jdk588\My Documents\ArcGIS\Default.gdb\MyTable'
arcpy.MakeQueryTable_management(theTable, "TableNameinTOC", "NO_KEY_FIELD")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Only limitation here is that QueryTables are in-memory, so they won't persist after the session has ended. So if you run calculations or manipulate the table in any way, you'll need to write the result out again using 'TableToTable_conversion()' in order to keep it.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:27:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-table-to-toc-through-script-10-0/m-p/433688#M34067</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2021-12-11T19:27:35Z</dc:date>
    </item>
  </channel>
</rss>

