Select to view content in your preferred language

Syntax to set FeatureLayer outfields

4151
9
02-14-2011 05:44 AM
DonFreeman
Emerging Contributor
Can someone clue me in on how to set FeatureLayer outfields programmatically? Something like
   FeatureLayer featureLayer = sender as FeatureLayer;
   featureLayer.OutFields = "*" ; 
       


Thanks
0 Kudos
9 Replies
JenniferNery
Esri Regular Contributor
The following are acceptable syntax for setting OutFields in code-behind, you can use one or the other as long as OutFields was not previously set to null.
FeatureLayer l = new FeatureLayer() { Url = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/MapServer/0" };
l.OutFields = new OutFields() { "incident_number", "description" }; //1- new instance with field names
l.OutFields.Add("*");//or 2- add field name or '*' to existing instance
0 Kudos
DonFreeman
Emerging Contributor
Thanks Jenn -
I was close to that but just couldn't quite get there. Now I have
 private void AllAvailableFeatureLayer_MouseLeftButtonUp(object sender, GraphicMouseButtonEventArgs args)
   {
   FeatureLayer featureLayer = sender as FeatureLayer;
   if (WebContext.Current.User.IsInRole("Administrator"))
    {
    featureLayer.OutFields.Add("*") ; 
    }

which I expected would add all of the fields to the FeatureDataForm if the user is an Administrator, but this doesn't work. Do I need to remove the original outfields or requery first?
Thanks
0 Kudos
JenniferNery
Esri Regular Contributor
Ideally, you will set OutFields before the layer gets Initialized and UpdateCompleted. If you will be showing all fields "*", it should be sufficient to do this upfront when you create the FeatureLayer.

Do you intend to change OutFields at runtime? Like maybe fewer fields showing in FeatureDataForm initially and then later have all fields show? I'm not sure what you are attempting to do here. If any of the query parameters on the layer changes (like Where, OutFields), you will need to call Update() for these changes to take effect.
0 Kudos
DonFreeman
Emerging Contributor
Jenn -
I want to show some fields to the casual user but ALL fields to the Administrator. So in the xaml outfields is set to the smaller set. But in the code behind I test for Administrator and expand the outfields set. I tried issuing
featureLayer.OutFields.Add("*") ; 
    featureLayer.Update() ;

but then NONE of the fields were visible in the featureDataForm.
0 Kudos
JenniferNery
Esri Regular Contributor
I think you can perform the check if user is Admin early on and set OutFields to all or select fields.

FeatureLayer l = new FeatureLayer()
{
 Url = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/MapServer/0"
};
if (user is Admin) //TODO: check if user is Admin
 l.OutFields = new OutFields() { "*" };
else
 l.OutFields = new OutFields() { "incident_number", "description" };
MyMap.Layers.Add(l);
0 Kudos
DonFreeman
Emerging Contributor
I would probably have to do that on page load or something like it early on. But what if the user was already on the page before he/she decided to login? Non Administrators are not required to login.
0 Kudos
JenniferNery
Esri Regular Contributor
Thank you for sharing us this use case. We are considering on accommodating it in future versions of our API.

Currently, FeatureDataForm does not update its layout when layer OutFields parameter changes. Current workaround is to set the FeatureLayer OutFields before setting FeatureDataForm's FeatureLayer property.

 featureLayer.OutFields = new OutFields() { "address" }; //update to OutFields ("*" or subset of fields)
 MyFeatureDataForm.FeatureLayer = featureLayer;
0 Kudos
DonFreeman
Emerging Contributor
Thanks Jenn -

I guess I will give up on trying to include this feature in my app. Also, the workaround you suggest does not work for some reason in my case. I already had the equivalent in my code which is:
  private void AllAvailableFeatureLayer_MouseLeftButtonUp(object sender, GraphicMouseButtonEventArgs args)
   {
   FeatureLayer featureLayer = sender as FeatureLayer;
   if (WebContext.Current.User.IsInRole("Administrator"))
    {
    featureLayer.OutFields.Add("*") ; 
    featureLayer.Update() ;
    }   

   for (int i = 0; i < featureLayer.SelectionCount; i++)
    featureLayer.SelectedGraphics.ToList().UnSelect();

   args.Graphic.Select();

   MyFeatureDataForm.FeatureLayer = featureLayer;
   MyFeatureDataForm.GraphicSource = args.Graphic;

   FeatureDataFormBorder.Visibility = Visibility.Visible;
   MyPopup.IsOpen=true;
   }

If you try it you will see that the DataForm comes up blank. Oh well . . . (shrug).
0 Kudos
JenniferNery
Esri Regular Contributor
Oh okay.

I tried the reverse where OutFields was initially "*" and then I changed OutFields to a subset of fields before setting MyFeatureDataForm.FeatureLayer. The workaround worked for this case.

For your case, you can do the following. Update() the layer once you have determined that the user is Admin and set FeatureLayer property on UpdateCompleted.
void featureLayer_UpdateCompleted(object sender, System.EventArgs e)
{
 FeatureLayer featureLayer = sender as FeatureLayer;
 MyFeatureDataForm.FeatureLayer = featureLayer;
}
//Do after login when user is determined to be Admin user.
private void Button_Click(object sender, RoutedEventArgs e) 
{
 FeatureLayer featureLayer = MyMap.Layers["PointLayer"] as FeatureLayer;
 featureLayer.OutFields = new OutFields() { "*" };
 featureLayer.UpdateCompleted += new System.EventHandler(featureLayer_UpdateCompleted);
 featureLayer.Update();
}
0 Kudos