Change Feature Layer Data Source URL

1379
2
Jump to solution
04-13-2018 01:41 PM
MKa
by
Occasional Contributor III

I have about 14 feature layers, and i want to be able to switch our the Data Source URL from my code if it does not match the environment I am in.  i would even settle for being able to just drop and re-add the layer if it has a different datasource then the one I want to load.  Unfortunately, I cant seem to find how to Get/Set this value for a layer?  I attached a screen shot of what I want to get/set

FeatureLayer LoadLayer = GetBasicFeaturLayer(entry.Key) as FeatureLayer;
                        
if (LoadLayer == null) //Not already on the Map, load it
{
   Uri uri = new Uri(NEWurl + entry.Value);
   LoadLayer = LayerFactory.Instance.CreateFeatureLayer(uri, MapView.Active.Map, position);
}
else
{
   //HERE
   //HERE
   //I need to check if the layer that already exists on the map has a different datasource
   //IF CURRENT URL != NEWurl
   //If Not equal, then remove existing layer or some how redo the data source   

   //Here is the remove logic, but I would rather rename, but I first need to find out if datasouce is different
   RemoveBasicFeaturLayer(entry.Key); 
   Uri uri = new Uri(NEWurl + entry.Value);
   LoadLayer = LayerFactory.Instance.CreateFeatureLayer(uri, MapView.Active.Map, position);
}

0 Kudos
1 Solution

Accepted Solutions
RichRuh
Esri Regular Contributor

The following code should allow you to reset the URL of a feature service layer.  The `newConnectionString` variable is your new url:

private static void FixDataConnectionLayer(Layer dataConnectionLayer, string newConnectionString)
{
  CIMStandardDataConnection dataConnection = dataConnectionLayer.GetDataConnection() as CIMStandardDataConnection;
  dataConnection.WorkspaceConnectionString = newConnectionString;
  dataConnectionLayer.SetDataConnection(dataConnection);
}

View solution in original post

0 Kudos
2 Replies
RichRuh
Esri Regular Contributor

The following code should allow you to reset the URL of a feature service layer.  The `newConnectionString` variable is your new url:

private static void FixDataConnectionLayer(Layer dataConnectionLayer, string newConnectionString)
{
  CIMStandardDataConnection dataConnection = dataConnectionLayer.GetDataConnection() as CIMStandardDataConnection;
  dataConnection.WorkspaceConnectionString = newConnectionString;
  dataConnectionLayer.SetDataConnection(dataConnection);
}
0 Kudos
MKa
by
Occasional Contributor III

That worked great.  Here is my updated code.  I do this as I fear someone will be working in test and mistakenly load a map that points to production.  This will ensure that the Test URL gets loaded instead of the production URL.  I know our environment on the ActiveMapViewChanged event and make sure the loaded layers have the correct url based on the environment we are in.

FeatureLayer LoadLayer = GetBasicFeaturLayer(entry.Key) as FeatureLayer;
                        
if (LoadLayer == null) //Not already on the Map, load it
{
   Uri uri = new Uri(NEWurl + entry.Value);
   LoadLayer = LayerFactory.Instance.CreateFeatureLayer(uri, MapView.Active.Map, position);
}
else //We need to check the datasource of the loaded map and make sure it is updated to correct path based on environment
{
   CIMStandardDataConnection dataConnection = LoadLayer.GetDataConnection() as CIMStandardDataConnection;
   string urlMatch = "URL=" + NEWurl;
   if (!dataConnection.WorkspaceConnectionString.Contains(urlMatch))
   {
      dataConnection.WorkspaceConnectionString = urlMatch;
      LoadLayer.SetDataConnection(dataConnection);
   }
}    
0 Kudos