Hi,
I've created feature layer from featureservice. I've almost 73 predefined edit templates and want to add couple of attributes to each template after layer is getting added on map.
I got all templates using below code but not sure how to get predefined attributes and set values.
IEnumerable<FeatureLayer> featLayers = theMap.GetLayersAsFlattenedList().OfType<FeatureLayer>();
foreach (FeatureLayer item in featLayers)
{
foreach (var template in MappingExtensions.GetTemplates(item))
{
//Define attribute for each template.
}
}
Any help would be appreciated.
-Prashant
Solved! Go to Solution.
This works for me:
private async Task UpdateTemplates(FeatureLayer fLayer)
{
// set the default value for the DisplayName field
// to whatever the name of the template is
await QueuedTask.Run(() =>
{
var layerDef = fLayer.GetDefinition() as CIMFeatureLayer;
var featTemplates = layerDef.FeatureTemplates?.ToList();
if (featTemplates == null)
featTemplates = new List<CIMEditingTemplate>();
foreach (var t in featTemplates.OfType<CIMFeatureTemplate>())
{
if (t.DefaultValues == null)
t.DefaultValues = new Dictionary<string, object>();
Debug.Print(t.Name);
foreach (var kvp in t.DefaultValues)
Debug.Print($"\t {kvp.Key} : {kvp.Value}");
t.DefaultValues["DisplayName"] = t.Name;
}
layerDef.FeatureTemplates = featTemplates.ToArray();
fLayer.SetDefinition(layerDef);
});
}
Did you try template.GetDefinition() to and then loop through each key value pair in CIMFeatureTemplate.DefaultValues , edit the definition, then call template.SetDefinition?
I'm able to assign values for CIMFeatureTemplate but when I call SetDefinition, values are resetting again and showing empty.
Here is my code:
IEnumerable<FeatureLayer> featLayers = theMap.GetLayersAsFlattenedList().OfType<FeatureLayer>();
foreach (FeatureLayer item in featLayers)
{
foreach (var template in MappingExtensions.GetTemplates(item))
{
CIMFeatureTemplate featTemplate = (CIMFeatureTemplate)template.GetDefinition();
featTemplate.DefaultValues["fieldname"] = "SomeValue"; //Values are updated
template.SetDefinition(featTemplate); //template.Defaultvlaues are empty here
}
}
This works for me:
private async Task UpdateTemplates(FeatureLayer fLayer)
{
// set the default value for the DisplayName field
// to whatever the name of the template is
await QueuedTask.Run(() =>
{
var layerDef = fLayer.GetDefinition() as CIMFeatureLayer;
var featTemplates = layerDef.FeatureTemplates?.ToList();
if (featTemplates == null)
featTemplates = new List<CIMEditingTemplate>();
foreach (var t in featTemplates.OfType<CIMFeatureTemplate>())
{
if (t.DefaultValues == null)
t.DefaultValues = new Dictionary<string, object>();
Debug.Print(t.Name);
foreach (var kvp in t.DefaultValues)
Debug.Print($"\t {kvp.Key} : {kvp.Value}");
t.DefaultValues["DisplayName"] = t.Name;
}
layerDef.FeatureTemplates = featTemplates.ToArray();
fLayer.SetDefinition(layerDef);
});
}
Thank you KirkKuykendall1,
Perfectly working.. used t.Defaultvalues["FieldName"] = "SomeValue" to update attributes in collection.
Really appreciate your help,
Prashant
Prashant,
Take a look at the following help topics for some concepts and examples to get you started
https://github.com/esri/arcgis-pro-sdk/wiki/ProConcepts-Editing#feature-templates
https://github.com/esri/arcgis-pro-sdk/wiki/ProGuide-Templates
other topics on templates
https://github.com/esri/arcgis-pro-sdk/wiki/ProSnippets-Editing#edit-templates
https://github.com/Esri/arcgis-pro-sdk-community-samples/tree/master/Editing/EditingTemplates
Thank you SeanJones,
Really helpful resources to understand templates in depth.
-Prashant