I am creating a number of layer object files outside of an MXD document (the layer is being copied from a separate layer object file outside of an MXD) and attempting to loop through each new lyr object and change its datasource to an SDE FeatureClass that matches the layer. Please find relevant code below:

    sym_layers = ['some.lyr', 'lyr.lyr', 'files.lyr']

    for fc in version_list:
        for item in sym_layers:
            special_case = os.path.join(m_path, version_desired, item)
            MakeFeatureLayer_management(os.path.join(m_path, version_prev, item), item)
            SaveToLayerFile_management(item, os.path.join(m_path, version_desired, item))
            env.workspace = os.path.join(m_path, version_desired)
            lyr = mapping.Layer(item)
            lyr.replaceDataSource(os.path.join(sde_path, fc), 'SDE_WORKSPACE')

Upon running this, I receive a ValueError which can be viewed below:

Traceback (most recent call last): File "{some_path}{some_script_name}.py", line 145, in lyr.replaceDataSource(os.path.join(sde_path, fc), 'SDE_WORKSPACE')

File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw)

File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy_mapping.py", line 681, in replaceDataSource return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))

ValueError: Layer: Unexpected error

I am definitely not implementing replaceDataSource correctly. Is it not possible to perform such an operation outside of an mxd document?

  • It's definitely possible to do this. I would try lyr.dataSource = first, and to do that to a file geodatabase feature class. If you get that working then you'll know your issue is with the harder ArcSDE connection syntax. – PolyGeo Nov 30 '18 at 22:25
  • Layers don't really exist outside a map document. Loading the layer file creates a Layer object in the active map document, even if is never saved. Note that SDE no longer exsists; the term of art is enterprise geodatabase. You seem to have left out the dataset_name parameter, which certainly doesn't help. – Vince Dec 1 '18 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.