Select to view content in your preferred language

Access SDE from Data Store in python toolbox

181
2
a week ago
sbrowneotogo
New Contributor

I have a toolbox that accesses a database through an SDE, which is stored locally. This works for local jobs, but I need to deploy this to an Enterprise portal. I've created a Data Store for the SDE, but I can't figure out how to now access the Data Store version of the SDE in a python toolbox. I can find the Data Store through gis.content.search(query='*',item_type="Data Store"). But how do I use it to access a GDB in the SDE?

I need to convert something like:

r'E:\ProProjects\my_project\SDE_connections\my_sde.sde\my_gdb'

To access the same SDE through the Data Store

0 Kudos
2 Replies
TonyAlmeida
Occasional Contributor II

Maybe with ArcGIS API? Untested.

 

import arcpy
from arcgis.gis import GIS
import os

def main():
    # Get parameters 
    portal_url = arcpy.GetParameterAsText(0)
    username = arcpy.GetParameterAsText(1)
    password = arcpy.GetParameterAsText(2)
    data_store_title = arcpy.GetParameterAsText(3)

    # Authenticate with the Enterprise portal
    gis = GIS(portal_url, username, password)

    # Search for the Data Store item
    data_store_items = gis.content.search(query='*', item_type="Data Store")

    # Find the specific Data Store by title
    data_store = next((item for item in data_store_items if item.title == data_store_title), None)

    if data_store is None:
        arcpy.AddError(f"Data Store with title '{data_store_title}' not found.")
        return

    # Get the connection properties
    connection_properties = data_store.properties.connectionString

    # Use arcpy to access the geodatabase
    arcpy.env.workspace = connection_properties

    # Example: List all feature classes in the geodatabase
    feature_classes = arcpy.ListFeatureClasses()
    arcpy.AddMessage(f"Feature Classes: {feature_classes}")

if __name__ == "__main__":
    main()

 

 

0 Kudos
sbrowneotogo
New Contributor

Thanks for the suggestion. I'm able to get the connection string but it looks like assigning it to the environment causes it to break (the ListFeatureClasses runs forever until I interrupt it)

0 Kudos