Select to view content in your preferred language

Edit FeatureLayer outside a Map

712
3
07-12-2010 03:19 AM
deleted-user-pYdfRjvSAbTJ
Deactivated User
is it possible to update a FeatureLayer via SaveEdits outside a Map?
I mean, can I load all the graphics of a FeatureLayer, add, remove, update graphics, without having to add the layer to a map?
I need to edit several FeatureLayer batch that are shown in my map in a DynamicService (and I don't want to add all these FeatureLayer to the map)

Thanks in advance
0 Kudos
3 Replies
egismazeika
Emerging Contributor
I've got similar problem too.
So is it a bug or just feature "by design"?
0 Kudos
deleted-user-pYdfRjvSAbTJ
Deactivated User
I found a workaround: you have to add the FeatureLayer to a dummy Map...
I know this is quite strange and I really don't like it, but it works.
Create a dummy Map:
Map map = new Map() {
    Extent = new Envelope() {
        SpatialReference = new SpatialReference(3857)
    }
};

Add the FeatureLayer to the Layers collection, then initialize the FeatureLayer and in the Initialized event call the Update method to load the data (to fill the Graphics collection).
At this point you have to work with the Graphics: adding, updating, deleting graphics is all you have to do. Then call the SaveEdits method that in turn calls the ApplyEdits method of the internal EditTask (yes, the FeatureLayer has internally an EditTask).
Hint: if you have to work only with a subset of features set the Where property of the FeatureLayer before calling Initialize and Update methods.
it works, but I don't like this way at all: we are forced to load the FeatureLayer graphics (or a subset of them), working on them, before applying edits, with EditTask this is not necessary avoiding a roundtrip to the REST service (you have only to specify adds, deletes and updates graphics collection to send to the server).
0 Kudos
PreetiMaske
Esri Regular Contributor
Yes it is possible to edit featureLayer outside a map as long as the featurelayer is not readonly. In the following example a feature is being added to a featurelayer not in the Map. The key is to *initialize* the featurelayer and make edits in Intialized event. See code below for reference.
private void UpdateFeatureLayer_Click(object sender, RoutedEventArgs e)
    {
      FeatureLayer Layertobeupdated = new FeatureLayer();
      Layertobeupdated.ID = "mylayer";
      Layertobeupdated.Url = "http://localhost/ArcGIS/rest/services/MontgomerySimple/FeatureServer/3";
      Layertobeupdated.AutoSave = false;     
      Layertobeupdated.Initialize();
      Layertobeupdated.Initialized += new EventHandler<EventArgs>(Layertobeupdated_Initialized);     
    }

void Layertobeupdated_Initialized(object sender, EventArgs e)
    {
      FeatureLayer fl = sender as FeatureLayer;
      Graphic g = new Graphic();
      g.Geometry = new ESRI.ArcGIS.Client.Geometry.MapPoint(509314.1673, 685143.8811);
      g.Attributes.Add("Name", "abc");
      g.Attributes.Add("Service_Details", "Test adding SL");
      g.Attributes.Add("Service_Type", "Testing");
      g.Attributes.Add("Tracking_Number", "100");

      fl.Graphics.Add(g);
      fl.SaveEdits();     
    }

Hope this helps!
Preeti

I found a workaround: you have to add the FeatureLayer to a dummy Map...
I know this is quite strange and I really don't like it, but it works.
Create a dummy Map:
Map map = new Map() {
    Extent = new Envelope() {
        SpatialReference = new SpatialReference(3857)
    }
};

Add the FeatureLayer to the Layers collection, then initialize the FeatureLayer and in the Initialized event call the Update method to load the data (to fill the Graphics collection).
At this point you have to work with the Graphics: adding, updating, deleting graphics is all you have to do. Then call the SaveEdits method that in turn calls the ApplyEdits method of the internal EditTask (yes, the FeatureLayer has internally an EditTask).
Hint: if you have to work only with a subset of features set the Where property of the FeatureLayer before calling Initialize and Update methods.
it works, but I don't like this way at all: we are forced to load the FeatureLayer graphics (or a subset of them), working on them, before applying edits, with EditTask this is not necessary avoiding a roundtrip to the REST service (you have only to specify adds, deletes and updates graphics collection to send to the server).
0 Kudos