Adding an element to the feature layer programmatically

3651
13
01-03-2012 06:49 AM
MarvinOconitrillo
New Contributor III
Hi, I was trying to add a new element into my featurelayer service programmatically, but when I save the editions, it seems that doesn't work because I can't see changes on the map neither the data.
I was trying to use on my code behind an editor declared on the xaml, getting an instance of the feature layer from there, and then adding my new element with an Graphic instance that I create before, so, I use the add method of the feature layer and after that the saveEdit method, and finally the refresh methos to refresh my map.
Can anybody help me, I would like to know what I'm doing wrong about that, I'm posting fragments of my code to show what I explained before.
Thanks.

*Fragment of the xaml declaring the editor

<Grid.Resources>
            <esri:Editor x:Key="Editor" 
                         Map="{Binding ElementName=Map}" 
                         LayerIDs="Poligonos"                          GeometryServiceUrl="http://crbd02/ArcGIS/rest/services/Geometry/GeometryServer"
                             />
</Grid.Resources>


*Fragment of the code declaring the Map and the feature layer
<!-- Map Control -->
        <esri:Map x:Name="Map" Background="White" WrapAround="true" IsLogoVisible="False" Extent="-1442632.89843668,-274130.672780001,8713596.00123668,5756744.12838">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseEnter">
                    <ei:ChangePropertyAction TargetName="myMenuItems" PropertyName="Visibility">
                        <ei:ChangePropertyAction.Value>
                            <Visibility>Collapsed</Visibility>
                        </ei:ChangePropertyAction.Value>
                    </ei:ChangePropertyAction>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <esri:FeatureLayer ID="Poligonos" Url="http://crbd02/ArcGIS/rest/services/DemosAPIs/Edicion_APIS/FeatureServer/0" AutoSave="False" x:Name="Poligonos"
                                Mode="OnDemand" ValidateEdits="True" DisableClientCaching="True"/>
        
</esri:Map>


*Fragment of the code adding the new element into the feature layer, the object gra have the graphic that I draw before and I want to add to the feature layer.
Graphic gra = null;

                gra = VentanaDerroteroEnPanel.FinalizarDibujo();
                PanelVentanaDerrotero.Visibility = System.Windows.Visibility.Collapsed;

                Editor editor = LayoutRoot.Resources["Editor"] as Editor;
                foreach (GraphicsLayer graphicsLayer in editor.GraphicsLayers)
                {
                    if (graphicsLayer is FeatureLayer)
                    {
                        FeatureLayer featureLayer = graphicsLayer as FeatureLayer;
                        if (featureLayer.ID == "Poligonos")
                        {
                            featureLayer.Graphics.Add(gra);
                            featureLayer.SaveEdits();
                            featureLayer.Refresh();
                        }
                    }
                }
0 Kudos
13 Replies
JenniferNery
Esri Regular Contributor
Have you looked at this SDK sample? http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#EditToolsExplicitSave

Editor is interactive. The Add command allows you to draw the geometry and give it default prototype attributes. Your code-snippet indicate that you are not using Editor.Add at all. I'm also not sure if FeatureLayer has already been initialized and updated before you add your graphic. Are you setting both geometry and attributes?  You can subscribe to FeatureLayer.EndSaveEdits and SaveEditsFailed events to see error messages if any. You can run Fiddler to see what edit is being committed when you call SaveEdits().
0 Kudos
MarvinOconitrillo
New Contributor III
I subscribed the SaveEditsFailed and It's giving me an error "Unable to complete operation". I saw the example you send me, but there are some things that I don't understand, for example, the save edits button is just binding with the function of the editor, is there some way that I can emulate that function programmatically? and if there is, would you give me an example?

I guess I'm not setting the geometry and the attributes, could you bring me an example of that?
0 Kudos
JenniferNery
Esri Regular Contributor
If you are not using Editor. You can add graphic and save edits using the following code. Note that FeatureLayer l is already Initialized and UpdateCompleted. If FeatureLayer.AutoSave=False (True by default), you need to call SaveEdits() to commit the edits.
   Graphic g = new Graphic()
   {
    Geometry = new MapPoint(x, y, MyMap.SpatialReference), 
    Symbol = LayoutRoot.Resources["MySymbol"] as Symbol
   };
   foreach(var f in l.LayerInfo.Fields)
   {
    if (f.Editable)
     g.Attributes[f.Name] = f.Nullable ? null : somedefaultvalue;
   }
   l.Graphics.Add(g);
   l.SaveEdits();
0 Kudos
MarvinOconitrillo
New Contributor III
Hi, I used the code you give me, but I'm still getting an error when I'm saving the edition, I don't know if there are another thing I should consider when I'm saving my edits, for example something like permissions, etc.

Following I show you how I used the code:

Here I get the feature layer after it is initialized and updated
private void Poligonos_UpdateCompleted(object sender, EventArgs e)
{
    layerPoligonos = (FeatureLayer)sender;
}


After that I try to insert the new graphic and the attributes; I also checked the graphic had a correct geometry and symbol when I'm trying to insert into the feature layer
Graphic gra = null;

gra = VentanaDerroteroEnPanel.FinalizarDibujo();

foreach (var f in layerPoligonos.LayerInfo.Fields)
{
  f (f.Editable)
  {
     switch(f.Name)
     {
        case "ID":
        gra.Attributes[f.Name] = 205;
        break;
        case "Descripcion":
        gra.Attributes[f.Name] = "prueba";
        break;
     }//fin switch
   }
}
layerPoligonos.Graphics.Add(gra);
layerPoligonos.SaveEdits();


The error said "Unable to complete the operation", and have the code 400.

Do you have any idea what could be the problem with the code?

Thanks.
0 Kudos
JenniferNery
Esri Regular Contributor
Can you run fiddler with your sample app? The service error could mean a number of things. It's possible that the geometry is not topologically correct or there were some missing attributes that need to be set. But first can you see if you are able to add feature and save edits using Editor (interactively)? If yes, you can try to see if you need to simpilify the geometry before saving: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Simplify
0 Kudos
MarvinOconitrillo
New Contributor III
Hi, I tested adding a feature using the editor (interactively) and worked correctly, so I discard the layer like the problem in my insertion, but while I was working with the editor I saw that the add button have a parameter, that let me especify the kind of feature I'm going to insert, and I would like to know if at the code that you gave me, I should specify in the same way the kind of feature that I'm going to insert before I use the add method.

Thanks.
0 Kudos
JenniferNery
Esri Regular Contributor
0 Kudos
MarvinOconitrillo
New Contributor III
Hi Jennifer,

My code is working right now, the problem was the way I built the Graphic I was trying to insert.

Thanks a lot
0 Kudos
sameerpuppal
Occasional Contributor
Hi,

I am facing exactly the same issue with the same error. can you share the way you built the graphics which worked ?

Regards,
Sameer puppal
0 Kudos