Cannot create connection string from .SDE file

01-14-2019 12:04 PM
New Contributor


I'm working on a script to read all the SDE connections from my Pro project to check whether this datasource is registered with my Arcgis Server. If the datasource is not registered, I want to fix this by adding the connection to Arcgis Server.

This script should avoid copying the data to Arcgis Server from my Enterprise Geodatabase when I publish a mapservice from an Arcpy Script. 

I found the following functions in the Python documentation: 

However, my script gives an error when I run the generate_connection_string function on the datastoremanager object. 

The datastoremanager object works fine when I list the datasources which are registered with Arcgis Server

I'm using Arcgis Pro 2.2.4 with arcgis python package 1.4.1 and Arcgis Enterprise 10.6.1

The following script gives the error creating the connectionwhen I run it from the Python console in Pro:

import arcgis.gis
import arcgis.gis.server
gis = arcgis.gis.GIS(url='', username='portaladmin',password='fakepassword')
server = arcgis.gis.server.Server(url='', gis=gis)
datastoremanager = server.datastores
<DataStoreManager for>
[<Datastore title:"" type:"egdb">, <Datastore title:"" type:"egdb">, <Datastore title:"" type:"egdb">, <Datastore title:"" type:"egdb">, <Datastore title:"" type:"nosql">]
Traceback (most recent call last):
 File "<string>", line 1, in <module>
 File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\server\admin\", line 315, in generate_connection_string
 if self._con.portal_connection:
AttributeError: '_ArcGISConnection' object has no attribute 'portal_connection'

I also tried server = arcgis.gis.server.Server(url='',  gis=gis), but this gives me the same error.

Any ideas why I get this error?

0 Kudos
2 Replies
New Contributor III

I'm running into the same problem. Below is my DataStoreManager object I am creating. Did you ever find a resolution to this?

dsm = server.DataStoreManager(ags_admin_url + '/admin/data', gis=gis)
conn_str = dsm.generate_connection_string(sde)

{AttributeError}'_ArcGISConnection' object has no attribute 'portal_connection'
0 Kudos
Occasional Contributor
I discussed the issue with a Esri Developer at the DevSummit in Palm Springs.
there are two problems, the first is at line 4 in my original code sample and line 2 in the sample below. As it turns out, you should not create your ArcGIS Server object this way, but instead get it from the Gis object, which contains a list of all federated servers in your ArcGIS Enterprise. However, the Gis object only contains the public URLS from the Federated ArcGIS Servers and if you disabled Administrative Access on the webadaptor, you cannot execute the publishing tools necessary for this operation.
If administrative access is enabled, get your ArcGIS Server from the GIS object, if it is not, or you run into some other issue, here is my workaround:
The example below should have Gis object (self gis), an ArcGIS Server url with administrative access and in this case I loop my Pro project layers to check if all database connections are there.  The workaround is in the ConvertSDEtoConnectionString function, where the SDE file is uploaded and a connection string is generated from that upload. This function is actually copied from the ArcGIS API for Python and modified to fix the failing upload. 
def RegisterDataSources(self,aprx,serverurl):
        gisserver = arcgis.gis.server.Server(url=serverurl , gis=self.gis) ##this is wrong!
        #servers = gis.admin.servers.list()
        registeredDatasources = gisserver.datastores.list()
        firstmap = aprx.listMaps("*")[0]
        for lyr in firstmap.listLayers("*"):
            if lyr.supports('DATASOURCE'):
                user =lyr.connectionProperties['connection_info']['user']
                database = lyr.connectionProperties['connection_info']['database']
                datasourcename = user + '@' + database
                for registeredDatasource in registeredDatasources:
                    path ='path')
                    if path.endswith(datasourcename):
              "Datasource already registered: " + datasourcename)
                        found = True
                if not found:

    def RegisterDataSourcefromAprx(self, lyr, datasourcename,gisserver):
        path = lyr.dataSource[:lyr.dataSource.rfind('\\')]
        connectionString = self.ConvertSDEtoConnectionString(path,gisserver)
        gisserver.datastores.add_database(datasourcename,connectionString)"Registered new datasource: " + datasourcename)
    def ConvertSDEtoConnectionString(self, path, gisserver):
        from arcgis.gis.server.catalog import ServicesDirectory
        from arcgis.gis.server import Uploads
        up = Uploads(url=gisserver.url.replace("admin", "admin/uploads"),

        d = ServicesDirectory(url=self.configparams['serverurl'])
        d._con = gisserver._con
            service = d.get("PublishingTools", 'System')
            service = d.get("PublishingToolsEx", 'System')
        upload_res = up.upload(path=path)
        if upload_res[0] == True:
            res = service.get_database_connection_string(
        return res
-Joël Hempenius.

Languages: JavaScript, Python and Dunglish