Select to view content in your preferred language

Assign Layer IDs using ArcPy

1685
3
Jump to solution
12-19-2023 12:01 PM
Labels (1)
JillianStanford
Frequent Contributor

Hi,

I'm using a python toolbox to process some data and create a map. I'll be using this map to overwrite an AGOL web layer.

I want to make sure that the layer IDs will be the same as in the hosted feature layer. In ArcGIS Pro, I would click the "Allow assignment of unique numeric IDs..." property of the map and then edit the Layer ID property of each layer.

I can see no way to accomplish this using arcpy. The map object doesn't seem to have an "Allow assignment" property and it is not a parameter of the createMap function I used to create the map. Layers also do not seem to have an ID property.

Does anyone know how to do this without user interaction?

Thanks!

Jill

0 Kudos
1 Solution

Accepted Solutions
victorCanut
Occasional Contributor

hi,

with Python CIM acces you can edit Layer ID in your script (Python CIM access—ArcGIS Pro | Documentation)

me i use it to assign Layer ID before publish like this :

import arcpy
aprx = arcpy.mp.ArcGISProject('current')
mp = aprx.listMaps('Name of the map')[0]
i = 10
for lyr in mp.listLayers():
    print (lyr.name)
    l_cim = lyr.getDefinition('V2')
    l_cim.serviceLayerID = i
    lyr.setDefinition(l_cim)
    print ("new ID ---> " + str(l_cim.serviceLayerID))
    i += 10

 

View solution in original post

3 Replies
victorCanut
Occasional Contributor

hi,

with Python CIM acces you can edit Layer ID in your script (Python CIM access—ArcGIS Pro | Documentation)

me i use it to assign Layer ID before publish like this :

import arcpy
aprx = arcpy.mp.ArcGISProject('current')
mp = aprx.listMaps('Name of the map')[0]
i = 10
for lyr in mp.listLayers():
    print (lyr.name)
    l_cim = lyr.getDefinition('V2')
    l_cim.serviceLayerID = i
    lyr.setDefinition(l_cim)
    print ("new ID ---> " + str(l_cim.serviceLayerID))
    i += 10

 

Clubdebambos
Frequent Contributor

I think this might be the only way to do it, but be careful using the CIM and be sure you know what you are doing. If you are on ArcGIS Pro 3.X, you use "V3" instead of "V2". If you export a layer to a layer file (.lyrx) and open in a notepad you can get a better understanding of the CIM. I recommend using Notepad ++ and changing the language to JSON for readability with syntax highlighting.

~ learn.finaldraftmapping.com
JillianStanford
Frequent Contributor

Thank you @victorCanut and @Clubdebambos 

That was exactly what I was looking for and I was not familiar with the CIM.

FYI, if it could help someone else, to enable the "Allow assignment of unique numeric IDs..." option I used the following:

map = aprx.createMap("MapName") 
m_cim = map.getDefinition('V3')
m_cim.useServiceLayerIDs = True
map.setDefinition(m_cim)

Thanks again!

Jill