Add to feature layer

2851
10
08-03-2011 10:24 AM
RyanDanford
New Contributor
How can I add a graphic to a feature layer without using the editor?

The new graphic will consist of geometry from a union thus the add command of the "Editor" is of no use.
0 Kudos
10 Replies
ChristopherHill
Occasional Contributor
Hello,

If you are trying to add graphics in the code behind:

myFeatureLayer.Graphics.Add(myGraphic);
0 Kudos
RyanDanford
New Contributor
I suppose that I should have been more clear in what I am trying to do.

private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            Draw draw = new Draw(map)
            {
                DrawMode = DrawMode.Rectangle,
                FillSymbol = new ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol() { BorderThickness = 2, BorderBrush = new SolidColorBrush(Colors.Red) },
                IsEnabled = true
            };
            draw.DrawComplete += (s, args) =>
                {
                    GeometryService reproj = new GeometryService(URL);
                    reproj.ProjectCompleted += (rep, done) =>
                    {
                        QueryTask qt = new QueryTask(URL);
                        qt.ExecuteCompleted += (task, res) =>
                            {
                                GeometryService geos = new GeometryService(URL);
                                geos.UnionCompleted += (geo, un) =>
                                    {
                                        FeatureLayer fl = (FeatureLayer)map.Layers["Layer"];
                                        Graphic g = new Graphic()
                                        {
                                            Geometry = un.Result,
                                            Symbol = ((SimpleRenderer)fl.Renderer).Symbol
                                        };
                                        fl.Graphics.Add(g);
                                        // Save back to SDE?
                                    };
                                if (res.FeatureSet.Count() > 0)
                                {
                                    geos.UnionAsync(res.FeatureSet.ToList<Graphic>());
                                }
                                draw.IsEnabled = false;
                            };
                        Query query = new Query();
                        query.OutFields.Add("*");
                        query.ReturnGeometry = true;
                        query.OutSpatialReference = map.SpatialReference;
                        query.Geometry = done.Results[0].Geometry;
                        qt.ExecuteAsync(query);
                    };
                    reproj.ProjectAsync(new List<Graphic>() { new Graphic() { Geometry = args.Geometry } }, new SpatialReference(3424));
                };
        }

The code above will add the graphic to the layer correctly, but I need to dump this new graphic back into the SDE.
0 Kudos
ChristopherHill
Occasional Contributor
If you have your feature layer set to AutoSave=true then you are done. if it is AutoSave=false then
fl.SaveEdits() would be your last call.

private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            Draw draw = new Draw(map)
            {
                DrawMode = DrawMode.Rectangle,
                FillSymbol = new ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol() { BorderThickness = 2, BorderBrush = new SolidColorBrush(Colors.Red) },
                IsEnabled = true
            };
            draw.DrawComplete += (s, args) =>
                {
                    GeometryService reproj = new GeometryService(URL);
                    reproj.ProjectCompleted += (rep, done) =>
                    {
                        QueryTask qt = new QueryTask(URL);
                        qt.ExecuteCompleted += (task, res) =>
                            {
                                GeometryService geos = new GeometryService(URL);
                                geos.UnionCompleted += (geo, un) =>
                                    {
                                        FeatureLayer fl = (FeatureLayer)map.Layers["Layer"];
                                        Graphic g = new Graphic()
                                        {
                                            Geometry = un.Result,
                                            Symbol = ((SimpleRenderer)fl.Renderer).Symbol
                                        };
                                        fl.Graphics.Add(g); // if AutoSave = true then it saves on add
                                        fl.SaveEdits(); // explicit save to the server when AutoSave = false
                                    };
                                if (res.FeatureSet.Count() > 0)
                                {
                                    geos.UnionAsync(res.FeatureSet.ToList<Graphic>());
                                }
                                draw.IsEnabled = false;
                            };
                        Query query = new Query();
                        query.OutFields.Add("*");
                        query.ReturnGeometry = true;
                        query.OutSpatialReference = map.SpatialReference;
                        query.Geometry = done.Results[0].Geometry;
                        qt.ExecuteAsync(query);
                    };
                    reproj.ProjectAsync(new List<Graphic>() { new Graphic() { Geometry = args.Geometry } }, new SpatialReference(3424));
                };
        }
0 Kudos
RyanDanford
New Contributor
Greetings...

I've tried using the SaveUpdates method, but nothing seems to happen.  I don't get any errors, but the data isn't saved either.  Is there something else that I need to do?  Is the graphic object missing some basic piece of required data?
0 Kudos
ChristopherHill
Occasional Contributor
It appears that the code is correct.

Make sure you are using the feature server url endpoint i.e. (FeatureLayer.Url = "http://.../FeatureServer/{LayerID}") the MapServer/{LayerID} end point is not for editing.

You can add event handler to the following FeatureLayer events.
FeatureLayer.BeginSaveEdits
FeatureLayer.EndSaveEdits
FeatureLayer.SaveEditsFailed
0 Kudos
RyanDanford
New Contributor
Make sure you are using the feature server url endpoint i.e. (FeatureLayer.Url = "http://.../FeatureServer/{LayerID}") the MapServer/{LayerID} end point is not for editing.


Ugh...  That was part of the problem.

I have another issue cropping up now when the graphic is added to the layer:


Type must be Int16.

   at ESRI.ArcGIS.Client.FeatureLayer.graphicAdded(Graphic g)
   at ESRI.ArcGIS.Client.FeatureLayer.Graphics_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
   at ESRI.ArcGIS.Client.GraphicCollection.InsertItem(Int32 index, Graphic item)
   at System.Collections.ObjectModel.Collection`1.Add(T item)
   at SJG.HMMFIELDBOOK.MainPage.<>c__DisplayClass1c.<HyperlinkButton_Click_1>b__19(Object geo, GeometryEventArgs un)
   at ESRI.ArcGIS.Client.Tasks.GeometryService.OnUnionCompleted(GeometryEventArgs args)
   at ESRI.ArcGIS.Client.Tasks.GeometryService.union_Completed(Object sender, RequestEventArgs e)
   at ESRI.ArcGIS.Client.Tasks.TaskBase.request_Completed(Object sender, RequestEventArgs e)
   at ESRI.ArcGIS.Client.WebRequest.OnComplete(RequestEventArgs args)
   at ESRI.ArcGIS.Client.WebRequest.downloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)
   at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e)
   at System.Net.WebClient.DownloadStringOperationCompleted(Object arg)
0 Kudos
deleted-user-hb3f0SCDAPf8
New Contributor II

Was the "Type must be Int16" resolved for you? How?

Thanks!

ReneeCammarere
Occasional Contributor

I've run into this same problem, and would be curious to know how you resolved it as well.

0 Kudos
JenniferNery
Esri Regular Contributor

You can use service metadata to get the correct datatype for each attribute. FeatureLayer.LayerInfo.Fields should have that information for you.

ArcGIS API for Silverlight API Reference| ArcGIS for Developers

var graphic = new Graphic() { Geometry =  someValidGeometry };

foreach(var f in layer.LayerInfo.Fields)

  if((f.OutFields.Contains("*") || f.OutFields.Contains(f.Name)) && f.Editable)

     f.Attributes[f.Name]= someValidValue; // check f.Type, f.Nullable, f.Length

But if you want to suppress validation, you can set FeatureLayer.ValidateEdits=false.

%%ItemTitle%%