problem setting FeatureLayer Where property at run time in code behind

787
6
12-07-2011 12:41 PM
markcheyne
Occasional Contributor
Hi - My app includes a feature layer. The app starts with all features in the layer shown. Later, I give the user a chance to restrict the features to a particular set, I do this by resetting the layer's Where property in a method in the code-behind, like this:

FeatureLayer siteLayer = Map.Layers["Sites"] as FeatureLayer;
siteLayer.Where = "PROPERTY_ID = 123";


This should cause all but one feature to disappear from the map. But nothing happens, the map appears unchanged.

Seeing http://forums.esri.com/Thread.asp?c=213&f=2455&t=299876 I add another line:

siteLayer.Update();


This causes all features in the map to be cleared. Further, If I try to do anything else in this method with the siteLayer reference (like get its extent so I can zoom to it), I now get a null reference exception.

I see from http://forums.arcgis.com/threads/40125-Is-there-a-feature-layer-quot-completed-quot-event that maybe I should wait to interact with the layer again until that event fires. It doesn't explain however why I either get no change in my map, or all my features disappear.

I also see the developer in that second post is doing something like removing and readding the layer. Is that necessary in my case, seems odd?

Ideas?

MC
0 Kudos
6 Replies
DominiqueBroux
Esri Frequent Contributor
It doesn't explain however why I either get no change in my map,

That's normal. You need to call Update after changing the Where clause. 'Update' will reexecute the request.


or all my features disappear.


I don't explain either, except obviously if your request returns nothing.
Are you sure that your criteria "PROPERTY_ID = 123" is supposed to return something?
You can test your query directly in your browser or use fiddler to look at the request sent, that should give a clue.
0 Kudos
markcheyne
Occasional Contributor
Hi, I really appreciate the feedback, thank you.

New day, fresh mind, I've got it sorted. Turns out I had a bug elsewhere. FYI, this is all that is necessary to immediately make the map filter its visible contents to the desired features:

FeatureLayer siteLayer = Map.Layers["Sites"] as FeatureLayer;
siteLayer.Where = "PROPERTY_ID = 123";
siteLayer.Update();

Thanks, MC
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Strange!!.
My best bet at this time is that your where clause is not working.
Could you use fiddler to look at the request sent?
Also could you try with a where clause 100% garantee such as "1=1"?

I tested with the Feature Layers Simple sample.
When I execute these lines of code:
  var featureLayer = MyMap.Layers["MyFeatureLayer"] as FeatureLayer;
   featureLayer.Where = "POP1990 > 50000";
   featureLayer.Update();

a new request is sent to the server and more cities are displayed (initial criteria was > 100000).


Note : I just noticed I had inversed 'Refresh' and 'Update' in my previous post. I fixed that.
0 Kudos
JerryBiedenbender
New Contributor
I am trying to do this same thing and every time I run my map It displays nothing. The code I am using is.

FeatureLayer siteLayer = MyMap.Layers["Dispatch"] as FeatureLayer;
            siteLayer.Where = "Status = Complete";
            siteLayer.Update();


My goal is actually to display everything that is not complete so I also tried

FeatureLayer siteLayer = MyMap.Layers["Dispatch"] as FeatureLayer;
            siteLayer.Where = "Status <> Complete";
            siteLayer.Update();



Neither of these work.

I also tried adding a where clause on the MainPage.xaml

<esri:FeatureLayer  DisableClientCaching="True" Where="Status = Complete" ID="Dispatch"   OutFields="*" x:Name="Dispatch"  MouseLeftButtonUp="FeatureLayer_MouseLeftButtonUp" Mode="OnDemand"   SelectionColor="Cyan"   Visible="True" Url="http://gis02/ArcGIS/rest/services/FeatureServices/Dispatch/FeatureServer/0"/>


This also did not work, It gave me a blank map.


Any advice on this?

Thanks,
Jerry
0 Kudos
markcheyne
Occasional Contributor
jerry - your experience seems to suggest problems with the Where expression? While I was troubleshooting, if I put my Where expression into the layer's XAML, I'd see what I wanted. Maybe confirm in ArcMap that your Where expression works on that layer?

Also, try removing the DisableClientCaching property, and set the layer Mode to "Snapshot"?
0 Kudos
JerryBiedenbender
New Contributor
I figured it out, I had a coded domain set up on my layer and it wanted the number value instead of the text value
0 Kudos