Select to view content in your preferred language

How to add graphics of point,line or polygon to feature layer in arcs api 2 by code?

4584
4
10-11-2010 07:21 AM
XiujuZhou
Emerging Contributor
I am working on ArcGIS API 2 silverlight application in environment of arcgis server 10,VS2010,silverlight 4. I need help to add features from graphics to feature layers by c# code instead of using Editorwiget. I draw a point, line or polygon on map, and need to add them to feature layers on  a feature service.
0 Kudos
4 Replies
dotMorten_esri
Esri Notable Contributor
Just create a new instance of graphic and add it to the Graphics collection on the featurelayer. If the layer is editable, it will automatically be saved back to the server:

var graphic = new Graphic();
graphic.Geometry = new MapPoint(12,34) { SpatialReference = new SpatialReference(4326) };
graphic.Attributes["Name"] = "Lorem Ipsum";
myFeatureLayer.Graphics.Add(graphic);
0 Kudos
XiujuZhou
Emerging Contributor
Thank you so much, SharpGIS! ! I tried your code, it works! You are really an expertise!

View Profile  View Forum Posts


Just create a new instance of graphic and add it to the Graphics collection on the featurelayer. If the layer is editable, it will automatically be saved back to the server:

var graphic = new Graphic();
graphic.Geometry = new MapPoint(12,34) { SpatialReference = new SpatialReference(4326) };
graphic.Attributes["Name"] = "Lorem Ipsum";
myFeatureLayer.Graphics.Add(graphic);
0 Kudos
DavidCarpenter
Frequent Contributor
Quick question just incase you are still monitoring this thread.  We have implemented the code snippet to try move a a line from a Graphics Layer to a Feature Layer and then through our Feature Access Service to the waiting Feature Class.  We are successfully moving the line(s) and it is being written to the feature class however......multiple lines have been added, none of which have been given an Object ID and a corresponding row in the attribute table in the Feature Class.  Also the attributes we are passing are not making it to the feature class.  I am not even sure how the lines can exist in the feature Class without a corresponding row but they do.

So bottom line is we have multiple lines in our feature class, none of which can be selected, none of which have attributes, and more fundamentally none of which have a corresponding row in the table.

Any ideas what we might be doing wrong?

Thanks
0 Kudos
DominiqueBroux
Esri Frequent Contributor
If you see graphics that don't have any corresponding row in the table, it might mean that you set 'AutoSave' to false.
In this case you have to call SaveEdits in order to save your graphics as rows in the database.
0 Kudos