Select to view content in your preferred language

How to "Define parameters in a query layer" on a feature class?

682
5
Jump to solution
07-11-2024 05:31 AM
nadja
by
Frequent Contributor

I'd like to incorporate this manual workflow into my dockpane,  but I don't find any useful documentation or samples. Does anyone know if it is possible and if yes, how it can be done?

my manual workflow:

I open an enterprise geodatabase feature class in a map in ArcGIS Pro. Then I open layer properties and go to source. there is the field "Query" (not definition query!) containing something like this "select attribute 1, attribute 2, ..., attribute n FROM layername".

nadja_swiss_parks_0-1720700834509.png

I then click on the pen on the right hand side and modify the query to "select * from layername". 

I found this documentation: https://pro.arcgis.com/en/pro-app/latest/help/mapping/layer-properties/define-parameters-in-a-query-...

but I'm trying to do it in a dockpane for any layer that i add to my map using this method: 

 

using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri(sde_path))))
{

    //ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(featureClassDefinition.ToString(), "featureClassDefinition");
    using (FeatureClass fc = geodatabase.OpenDataset<FeatureClass>("parcs." + name))
    {
        // Create the feature layer creation parameters
        FeatureLayerCreationParams layerCreationParams = new FeatureLayerCreationParams(fc)
        {
            // Optionally set additional properties
            Name = name
        };


        // Create the feature layer and add it to the active map
        await QueuedTask.Run(() => LayerFactory.Instance.CreateLayer<FeatureLayer>(layerCreationParams, MapView.Active.Map));
    }
}

 

how can i add the layer such that its query says "select * from layername"? any help is greatly appreciated.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi,

You can create layer with geoprocessing tool Make Query Layer. It creates a query layer from a DBMS table based on an input SQL select statement. So, you don't need your code above. How to call geoprocessing tool you can find here. Tool name for calling would be "management.MakeQueryLayer"

View solution in original post

0 Kudos
5 Replies
Aashis
by Esri Contributor
Esri Contributor

Have you looked into the QueryDescription Class, which is used to create a table or feature class representing a query layer? 

Sample code snippets

 

GKmieliauskas
Esri Regular Contributor

Hi,

You can create layer with geoprocessing tool Make Query Layer. It creates a query layer from a DBMS table based on an input SQL select statement. So, you don't need your code above. How to call geoprocessing tool you can find here. Tool name for calling would be "management.MakeQueryLayer"

0 Kudos
pocalipse
Regular Contributor

In line 16 when you have the layer it is pretty simple:

 

var queryName = "My Special Query";
var whereClause = "1 = 1";
layer.InsertDefinitionQuery(new DefinitionQuery(queryName, whereClause), true);

 

 

Since you are creating a new layer you don't have to check if the query already exist but that is also quite simple.

 

var q = layer.DefinitionQueries.SingleOrDefault(e => e.Name.Equals(queryName, StringComparison.CurrentCultureIgnoreCase));

 

 

Complete:

// Create the feature layer and add it to the active map
await QueuedTask.Run(() => {
    var layer = LayerFactory.Instance.CreateLayer<FeatureLayer>(layerCreationParams, MapView.Active.Map)

    var queryName = "My Special Query";
    var whereClause = "1 = 1";
    layer.InsertDefinitionQuery(new DefinitionQuery(queryName, whereClause), true);
});
nadja
by
Frequent Contributor

Thank you, that code snippet works, but unfortunately it creates a definition query, whereas I meant the query under layer properties >> source >> query...

0 Kudos
UmaHarano
Esri Regular Contributor

Here is a code snippet that creates a Query Layer - with the "SqlQuery" defined.  Create a query layer 

await QueuedTask.Run(() =>
{
  Map map = MapView.Active.Map;
  Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri(@"C:\Connections\mySDE.sde")));
  CIMSqlQueryDataConnection sqldc = new CIMSqlQueryDataConnection()
  {
    WorkspaceConnectionString = geodatabase.GetConnectionString(),
    GeometryType = esriGeometryType.esriGeometryPolygon,
    OIDFields = "OBJECTID",
    Srid = "102008",
    SqlQuery = "select * from MySDE.dbo.STATES",
    Dataset = "States"
  };
  var lcp = new LayerCreationParams(sqldc)
  {
    Name = "States"
  };
  FeatureLayer flyr = LayerFactory.Instance.CreateLayer<FeatureLayer>(lcp, map);
});
0 Kudos