Solved! Go to Solution.
... the tool runs fine if I don't specify the Fields collection. But then the output doesn't contain the fields of the input features; it contains the fields of the schema set in the GPK; which is the problem I'm trying to resolve.!
/*
* Build the GP Parameters...
*/
// We want to specify input attributes - create a new FeatureSet.
FeatureSet featureSet = new FeatureSet();
// Create the Fields and add one called "VALUE".
featureSet.Fields = new List<Field> { new Field() { FieldName = "VALUE", Type = Field.FieldType.String, Alias = "VALUE" } };
// Create the graphic to submit.
Graphic g = new Graphic() { Geometry = _mercator.ToGeographic(e.MapPoint) };
// Add the graphic to the FeatureSet
featureSet.Features.Add(g);
// Set the graphic's attribute
featureSet.Features[0].Attributes["VALUE"] = "Hello";
// Create a new list of GPParameters
List<GPParameter> parameters = new List<GPParameter>();
// Add a new GPFeatureRecordSetLayer to the parameter list, passing in the FeatureSet created above.
parameters.Add(new GPFeatureRecordSetLayer("Input_Features",featureSet));
// Handler for the completed event
_gpTask.ExecuteCompleted += (s, e1) =>
{
//OutputCopyFeatures
GPExecuteResults results = e1.Results;
GPFeatureRecordSetLayer rs = results.OutParameters[0] as GPFeatureRecordSetLayer;
// Check the attributes... output feature should have attribute Field "VALUE" with value "Hello".
};
var featureSet = new FeatureSet();
var layer = GetSelectedInputLayer();
featureSet.Fields = new List<Field>((layer as FeatureLayer).LayerInfo.Fields);
foreach (var graphic in layer.SelectedGraphics)
{
featureSet.Features.Add(graphic);
}
parameters.Add(new GPFeatureRecordSetLayer("InputFeatures", featureSet));
parameters.Add(new GPLinearUnit("Distance", esriUnits.esriMeters, Convert.ToDouble(distanceText.Text)));
public static ICollection<Field> GetFields(Graphic graphic)
{
var fields = new List<Field>();
foreach (var attribute in graphic.Attributes.Keys)
{
fields.Add(new Field()
{
FieldName = attribute, Type = Field.FieldType.Unknown, Alias = attribute
});
}
return fields;
}
public static ICollection<Field> GetFields(Layer layer)
{
if (layer is FeatureLayer)
{
var featureLayer = layer as FeatureLayer;
if (featureLayer.LayerInfo != null)
{
return featureLayer.LayerInfo.Fields;
}
else if (featureLayer.Graphics.Count > 0)
{
return GetFields(featureLayer.Graphics[0]);
}
}
else if (layer is GraphicsLayer)
{
var graphicsLayer = layer as GraphicsLayer;
if (graphicsLayer.Graphics.Count > 0)
{
return GetFields(graphicsLayer.Graphics[0]);
}
}
return new List<Field>();
} public static ICollection<Field> GetFields(Graphic graphic) { var fields = new List<Field>(); foreach (var attribute in graphic.Attributes) { var field = new Field() { FieldName = attribute.Key, Alias = attribute.Key }; if (attribute.Key.ToLower() == "shape") { field.Type = Field.FieldType.Geometry; } else if (attribute.Key.ToLower() == "fid") { field.Type = Field.FieldType.OID; } else if (attribute.Key.ToLower() == "objectid") { field.Type = Field.FieldType.OID; } else if (attribute.Value is int) { field.Type = Field.FieldType.Integer; } else if (attribute.Value is double) { field.Type = Field.FieldType.Double; } else if (attribute.Value is float) { field.Type = Field.FieldType.Single; } else if (attribute.Value is DateTime) { field.Type = Field.FieldType.Date; } else { field.Type = Field.FieldType.String; } fields.Add(field); } return fields; }