Select to view content in your preferred language

Set attributes in featurelayer edittemplate

1797
6
Jump to solution
03-01-2021 11:08 PM
PrashantKirpan
Frequent Contributor

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 

0 Kudos
1 Solution

Accepted Solutions
KirkKuykendall1
Frequent Contributor

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);
    });
}

View solution in original post

6 Replies
KirkKuykendall1
Frequent Contributor

Did you try template.GetDefinition() to and then loop through each key value pair in CIMFeatureTemplate.DefaultValues , edit the definition, then call template.SetDefinition?

0 Kudos
PrashantKirpan
Frequent Contributor

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
  }
}

 

0 Kudos
KirkKuykendall1
Frequent Contributor

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);
    });
}
PrashantKirpan
Frequent Contributor

Thank you KirkKuykendall1,

Perfectly working.. used t.Defaultvalues["FieldName"] = "SomeValue" to update attributes in collection.

Really appreciate your help,

Prashant

0 Kudos
PrashantKirpan
Frequent Contributor

Thank you SeanJones,

Really helpful resources to understand templates in depth.

-Prashant

 

0 Kudos