ArcGIS Pro 3.0 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / Layer Class / FindAndReplaceWorkspacePath Method
A string that represents the workspace path or connection file you want to find. If an empty string is passed, then the workspace path will be replaced with the replaceWorkspacePath parameter depending on the value of the validate parameter.
A string that represents the workspace path or connection file you want to replace to.
(Optional) -true- the workspace will only be updated if the replaceWorkspacePath value is a valid workspace. If it is not valid, the workspace will not be replaced.
-false- the method will set the workspace to match the replaceWorkspacePath, regardless of a valid match. In this case, if a match does not exist, then the layer would appear broken.
(default value = true)
Example

In This Topic
    FindAndReplaceWorkspacePath Method (Layer)
    In This Topic
    Performs a find and replace of workspace path for the layer. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public void FindAndReplaceWorkspacePath( 
       string findWorkspacePath,
       string replaceWorkspacePath,
       bool validate
    )
    Public Sub FindAndReplaceWorkspacePath( _
       ByVal findWorkspacePath As String, _
       ByVal replaceWorkspacePath As String, _
       Optional ByVal validate As Boolean _
    ) 

    Parameters

    findWorkspacePath
    A string that represents the workspace path or connection file you want to find. If an empty string is passed, then the workspace path will be replaced with the replaceWorkspacePath parameter depending on the value of the validate parameter.
    replaceWorkspacePath
    A string that represents the workspace path or connection file you want to replace to.
    validate
    (Optional) -true- the workspace will only be updated if the replaceWorkspacePath value is a valid workspace. If it is not valid, the workspace will not be replaced.
    -false- the method will set the workspace to match the replaceWorkspacePath, regardless of a valid match. In this case, if a match does not exist, then the layer would appear broken.
    (default value = true)
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Example
    Change the underlying data source of a feature layer - same workspace type
    //This is the existing layer for which we want to switch the underlying datasource
    var lyr = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
    QueuedTask.Run(() =>
    {
      var connectionStringToReplace = lyr.GetFeatureClass().GetDatastore().GetConnectionString();
      string databaseConnectionPath = @"Path to the .sde connection file to replace with";
      //If the new SDE connection did not have a dataset with the same name as in the feature layer,
      //pass false for the validate parameter of the FindAndReplaceWorkspacePath method to achieve this. 
      //If validate is true and the SDE did not have a dataset with the same name, 
      //FindAndReplaceWorkspacePath will return failure
      lyr.FindAndReplaceWorkspacePath(connectionStringToReplace, databaseConnectionPath, true);
    });
    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 8.1

    See Also