Select to view content in your preferred language

Unable to save standalone table to .lyrx using python

757
4
04-11-2022 03:07 PM
NathanEmerson
New Contributor II

I am trying to automate packaging of layer and other files to a .zip for sharing. This works fine for layers in the map using arcpy.management.SaveLayerToFile(),However this does not work when passing a table. 

Outside of Python I can right click on a standalone table in the content pane, and use Sharing > Save As Layer File. This works as expected and creates a .lyrx with the standalone table. Can I replicate this function using Python?

As an example I have attempted the following which returns the error 'The value is not a layer'

import arcpy
project = arcpy.mp.ArcGISProject(<path to project>)
map = project.listMaps()[0]
table = map.listTables()[0]
arcpy.management.SaveToLayerFile(table,<path to lyrx>)

Is there another function to save a table to a lyrx? or can I reference the table in another way so that .SaveToLayerFile will accept it?

Cheers!

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

Was there a chance that your table was associated with a featureclass in the past?

There is no mention of pure tables in the documentation.

Save To Layer File (Data Management)—ArcGIS Pro | Documentation

 


... sort of retired...
0 Kudos
NathanEmerson
New Contributor II

No, these tables are pure data related to features across multiple feature classes by a common id, no joins or relationships have previously or currently exist.

0 Kudos
DanPatterson
MVP Esteemed Contributor

It does work, as you indicated.... I wasn't expecting this ( I usually share a project package). 

I suspect you have to check in the CIM class for specifics for keywords and potential code possibilities.

Open the *.lyrx in notepad++ and you can check the contents.

 

{
  "type" : "CIMLayerDocument",
  "version" : "2.9.0",
  "build" : 32739,
  "tables" : [
    "CIMPATH=birdondog/spyder.xml"
  ],
  "tableDefinitions" : [
    {
      "type" : "CIMStandaloneTable",
      "name" : "Spyder",
      "uRI" : "CIMPATH=birdondog/spyder.xml",
      "sourceModifiedTime" : {
        "type" : "TimeInstant"
      },
      "useSourceMetadata" : true,
      "displayField" : "Spyder_required",
      "editable" : true,
      "dataConnection" : {
        "type" : "CIMStandardDataConnection",
        "workspaceConnectionString" : "DATABASE=..\\Arc_projects\\Test_29\\Test_29.gdb",
        "workspaceFactory" : "FileGDB",
        "dataset" : "Spyder",
        "datasetType" : "esriDTTable"
      },
      "autoGenerateRowTemplates" : true,
      "serviceTableID" : -1,
      "showPopups" : true
    }
  ],
  "rGBColorProfile" : "sRGB IEC61966-2.1",
  "cMYKColorProfile" : "U.S. Web Coated (SWOP) v2"
}

 

Note, it doesn't contain the data, just a reference to the location of the table (line 21) and the map frame where the standalone table was located.

 


... sort of retired...
0 Kudos
NathanEmerson
New Contributor II

Thanks for the details, I am only after a reference to the tables. I will look at the details and see if I can work something out. Cheers. 

0 Kudos