Publish a FGDB to AGOL - use layer alias instead of name

764
4
Jump to solution
01-03-2023 05:45 PM
Labels (1)
CassKalinski
Occasional Contributor

Short story:
I would like AGOL to use the layer alias names, not the feature class names, when I publish from a file GDB.

Long story:
I am publishing a set of 20 feature classes contained in a file GDB to AGOL. This ends up as a feature layer collection after processing. The online layers are created with the feature class names instead of the alias. I would like the online layers to use the feature class alias as there are spaces and punctuation in the names that the users prefer. That punctuation is not allowed in the feature class names in the GDB but is OK for the AGOL names.

Also, the AGOL feature service will subsequently be updated with an overwrite, so the names either need to be 'sticky' or the same question goes to the overwrite process. How to use the alias name instead of the feature class name?

Publish code I am using...

 

gis = GIS(userName, passWord)

service_properties = {
    'title': 'A Name',
    'type': 'File Geodatabase',
    'itemType': 'file',
    'tags': ['tags'],
    'typeKeywords': ['File', 'Geodatabase'],
    'description': 'A description',
    'snippet': 'A snippet',
    'spatialReference': '3857'
    }

content_zipped = r'C:\ZippedUpFileGDB.zip'

new_item = gis.content.add(
    item_properties = service_properties,
    data = content_zipped
    )

publish_properties = {
    'name': 'Service name',
    'description': 'Description',
    'layerInfo': {"capabilities": 'Query'},
    'targetSR': {"wkid": 3857}
    }

published_item = new_item.publish(
    publish_parameters = publish_properties,
    file_type = 'filegeodatabase',
    overwrite=True
    )

 

 

Update/overwrite code:

 

fs_id = 'TheGUID'

content_zipped = r'C:\ZippedUpFileGDB.zip'

agol_item = gis.content.get(fs_id)
agol_flc = FeatureLayerCollection.fromitem(agol_item)
results = agol_flc.manager.overwrite(content_zipped)

 

 

I know I can rename the AGOL layers like this...

 

fs_id = 'TheGUID'
agol_item = gis.content.get(fs_id)
fs_layers = agol_item.layers

lyr_renames = {'fc_name': 'fc_alias'}

for layer in fs_layers:
    current_name = layer.properties.name

    if current_name in lyr_renames:
        update_name = {
            "name": lyr_renames[current_name]
            }
        response = layer.manager.update_definition(update_name)

 

 

...but that breaks the update/overwrite.

 

0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
Occasional Contributor III

Hi @CassKalinski,

An alternative that I use is to have a template APRX (or mxd) that contains the data, instead of publishing as a gdb, publish directly as a feature service from the APRX/mxd using arcpy. The layer names maintain the alias names (the names as shown in the table of contents). And by doing this you can maintain symbology if needed. 

~ learn.finaldraftmapping.com

View solution in original post

0 Kudos
4 Replies
Clubdebambos
Occasional Contributor III

Hi @CassKalinski 

The rename code doesnt have to break the update/overwrite.

You can create a dictionary to store the feature class name as a key and the alias as a value. Add this at the start of your script.

 

import arcpy

gdb = r"path\to\.sde"

arcpy.env.workspace = gdb

## dictionary key-fc name, value-fc alias
lyr_renames = {fc:arcpy.da.Describe(fc)["aliasName"] for fc in arcpy.ListFeatureClasses()}

 

You can use the published_item variable to get the item id (TheGUID) for the content item, and add the rename code at the end of the script as a continuation.

 

agol_item = gis.content.get(published_item.id)
fs_layers = agol_item.layers

for layer in fs_layers:
    current_name = layer.properties.name

    if current_name in lyr_renames:
        update_name = {
            "name": lyr_renames[current_name]
            }
        response = layer.manager.update_definition(update_name)

 

 

~ learn.finaldraftmapping.com
0 Kudos
CassKalinski
Occasional Contributor

Hello @Clubdebambos,

Thank you for the reply. I think the section of my original post where I rename the layers (third code block) is essentially the same as what you have provided. My bad, I was inconsistent on naming and did not call out one abbreviation detail.

My line...
fs_id = 'TheGUID'
...is for the published feature service item. Think the rest then aligns with what you suggested.

And my...
lyr_renames = {'fc_name': 'fc_alias'}
...was meant to be just a placeholder for a full dictionary, just like you proposed. I compiled it when building the GDB/zip file that was published, but effectively the same. I should have noted that in the post.

 

If I am understanding the AGOL dynamics correctly, it is a bit circular. I can rename the layers in the feature service, but the next overwrite is dropping another copy of the GDB, with the old names (because of the same naming restrictions as the original). That clashes with the renamed feature service.

What I was hoping, in part, that there was a similar name/alias relationship in the published feature class that I could exploit. Or a way of forcing the publish and overwrite to use the GDB aliases. For now, our work around is to create a view that has the naming the users prefer.

0 Kudos
Clubdebambos
Occasional Contributor III

Hi @CassKalinski,

An alternative that I use is to have a template APRX (or mxd) that contains the data, instead of publishing as a gdb, publish directly as a feature service from the APRX/mxd using arcpy. The layer names maintain the alias names (the names as shown in the table of contents). And by doing this you can maintain symbology if needed. 

~ learn.finaldraftmapping.com
0 Kudos
CassKalinski
Occasional Contributor

The APRX route is where we ended up going. Thanks!