1

Using ESRI Silverlight API 3.1, .NET Framework 4.0, ArcGIS Server 10.0

Is there a way to update an attribute in the code behind without looping through the featureLayer.Graphics? Or a way to call FeatureLayer.Update without losing the edits made? We cannot hard code our layers into the xAML because we need them to be dynamic.

Ideally we would like to run something similar to a QueryTask, returning a set of features that we could update the attributes of.

To work around this we have attempted to use the FeatureLayer where and update functions. For this test there is only a single FeatureLayer in my map.

The where and update successfully revises the layer to have a single entry -

FeatureLayer testLayer = (ESRI.ArcGIS.Client.FeatureLayer)featureLayer;
testLayer.UpdateCompleted += UpdateTask_ExecuteCompleted;
testLayer.Where = "TEST_ID=12345";
testLayer.Update();

// Update Event

private void UpdateTask_ExecuteCompleted(object sender, System.EventArgs args)
{
  foreach (GraphicsLayer graphicsLayer in testEditor.GraphicsLayers)
  {
    if (graphicsLayer is FeatureLayer)
    {
      FeatureLayer featureLayer = graphicsLayer as FeatureLayer;

      foreach (Graphic feature in featureLayer.Graphics)
      {
        if (Convert.ToInt32(feature.Attributes["TEST_ID"]) == 12345)
         {
           feature.Attributes["NAME"] = "Renamed";
         }
      }

   }
}

This event handler will loop through my feature layer – now queried to 1 record, and update the name attribute.

The issue at this point is that on my display I now only have the single record showing. If I reset the where clause to “1=1” and run an update I lose my edit.

I can SaveEdits before I reset the layer which will save my edit. But I am hoping for a solution that will still allow the user to save all the edits at a single time when they are finished. Is there a way to update and maintain my edit?

This all is a workaround due to not being able to query to get the feature to edit. If there is a way of doing that without iterating through every Graphic that would be the ideal solution to this. Are we missing the big picture here?

1 Answer 1

0

Well all our searching and trying stuff over the last two weeks finally paid off and we found out how to edit attributes of a queried feature using the Silverlight API/.NET/LINQ/REST Feature Services.

I'm posting the code here in case someone else is struggling with this issue. The tricky bit is that the queried object (FeatureSet Graphic) is different from the FeatureLayer Graphic object so you need to retrieve the objectID from the queried object and use that to make a LINQ call to obtain the object from the FeatureLayer.Graphics collection.

 private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
  {
        FeatureSet featureSet = args.FeatureSet;

        if (featureSet != null && featureSet.Features.Count > 0)
        {
            FeatureLayer featureLayer = null;

            foreach (GraphicsLayer graphicsLayer in rackEditor.GraphicsLayers)
            {
                if (graphicsLayer is FeatureLayer)
                {
                    featureLayer = graphicsLayer as FeatureLayer;
                }
            }

            foreach (Graphic feature in featureSet.Features)
            {
                 object id = feature.Attributes[objId];
                 string carId = feature.Attributes["F_ID"].ToString();

                 Graphic graphic = (from a in featureLayer.Graphics
                                    where a.Attributes["F_ID"].ToString() == carId
                                    select a).FirstOrDefault();
                 graphic.Attributes["NAME"] = "Rename LINQ";
            }
      }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.