ArcGIS Pro 3.4 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / LayerFactory Class / CanCreateLayer<T> Method
Expected Layer Type
Can be one of LayerCreationParams derived objects.
A map or group layer instance to which the Layer will be added.
Example Version

CanCreateLayer<T> Method (LayerFactory)
Determines if a new Layer instance using the specified LayerCreationParams can be created and added to the specified container. This method must be called on the MCT. Use QueuedTask.Run.
Syntax

Parameters

layerParams
Can be one of LayerCreationParams derived objects.
container
A map or group layer instance to which the Layer will be added.

Type Parameters

T
Expected Layer Type

Return Value

A boolean indication that the layer instance corresponding to type T can be created.
Exceptions
ExceptionDescription
This method or property must be called within the lambda passed to QueuedTask.Run.
LayerCreationParams or container is null.
Cannot create the layer of the specified type with the set of LayerCreationParams.
Example
Using LayerFactory.Instance.CanCreateLayer with KG Create Layer Params
QueuedTask.Run(() =>
{
  //Feature class and/or standalone tables representing KG entity and
  //relate types can only be added to a map (or link chart) as a child
  //of a KnowledgeGraph layer....

  //For example:
  var fc = kg.OpenDataset<FeatureClass>("Some_Entity_Or_Relate_Name");
  try
  {
    //Attempting to create a feature layer containing the returned fc
    //NOT ALLOWED - can only be a child of a KG layer
    var fl_params_w_kg = new FeatureLayerCreationParams(fc);
    //CanCreateLayer will return false
    if (!(LayerFactory.Instance.CanCreateLayer<FeatureLayer>(
      fl_params_w_kg, map)))
    {
      System.Diagnostics.Debug.WriteLine(
        $"Cannot create a feature layer for {fc.GetName()}");
      return;
    }
    //This will throw an exception
    LayerFactory.Instance.CreateLayer<FeatureLayer>(fl_params_w_kg, map);
  }
  catch (Exception ex)
  {
    System.Diagnostics.Debug.WriteLine(ex.ToString());
  }

  //Can only be added as a child of a parent KG
  var dict = new Dictionary<string, List<long>>();
  dict.Add(fc.GetName(), new List<long>());
  var kg_params = new KnowledgeGraphLayerCreationParams(kg)
  {
    Name = $"KG_With_Just_{fc.GetName()}",
    IsVisible = false,
    IDSet = KnowledgeGraphLayerIDSet.FromDictionary(kg, dict)
  };
  var kg_layer = LayerFactory.Instance.CreateLayer<KnowledgeGraphLayer>(
    kg_params, map);

});
Requirements

Target Platforms: Windows 11, Windows 10

ArcGIS Pro version: 3.3 or higher.
See Also