Has anyone successfully called "addToDefinition" for a feature layer?

2873
2
11-26-2013 07:23 AM
BrianBerdel
New Contributor III
Hello,

I am trying to call the REST service "addToDefinition" from within my C# code for a feature layer (that requires a token) that I havepublished to my ArcGIS online account.

I am using the RestSharp library to instantiate/manage the REST call.

This is the API reference provided by ESRI:

http://resources.arcgis.com/en/help/arcgis-rest-api/#/Add_To_Definition_Feature_Layer/02r30000022800...

The problem is that I keep getting "error code: 400 Object reference not set to an instance of an object".

I have verified that I am pointing at the correct URL for updating the feature layer:

http://services.arcgis.com/MyOrganizationID/arcgis/admin/services/LayerName.FeatureServer/0/addToDef...

It looks as if the "f" and "token" parameters cannot be part of the request body, or the service will not find them. So I have added them as parameters to the URL of the REST service, and everything is getting through at that level.

The problem is that when I add the "fields" parameter to the body of the request, I get the error 400 code. I have tried adding it as a post parameter, a serialized object, a serialized string, but I keep getting the same error. It's almost as if the parameter isn't being accepted by the service.

When I copy and paste the same "fields" parameter my C# is trying to pass to the REST service directly into the REST parameter for on the services.arcgis.com link, it works just fine.

Has anyone come across this before/been able to successfully call this REST service from C# code?

Any help would be greatly appreciated.

Thanks.
0 Kudos
2 Replies
AnttiKajanus1
Occasional Contributor III
Hi,

There is similar issue going in this thread : http://forums.arcgis.com/threads/94407-FeatureServer-addToDefinition-successful-submission

H
ow I see it going is that we can change only properties that are not related how data is stored. Ie. we can change amount of features returned but we cannot add an field or an alias. There is no comments on this in the documentation but this is how I find it working by playing around with the options.

It would be good to have a clarification to this.
0 Kudos
BrianBerdel
New Contributor III
I just figured this out.

Here's what I did:

I made a class to represent the Esri Field object:

[Serializable]
public class ESRIFeatureLayerFieldDefinition
{
public string name { get; set; }
public string type { get; set; }
public string alias { get; set; }
public string sqlType { get; set; }
public int length { get; set; }
public bool nullable { get; set; }
public bool editable { get; set; }
public string domain { get; set; }
public string defaultValue { get; set; }
}

from there I created a test field and added it to a list:

ESRIFeatureLayerFieldDefinition addField = new ESRIFeatureLayerFieldDefinition();

addField.name = "TestField14";
addField.type = "esriFieldTypeString";
addField.alias = "TestField14";
addField.sqlType = "sqlTypeOther";
addField.length = 150;
addField.nullable = true;
addField.editable = true;
addField.domain = null;
addField.defaultValue = null;


List<ESRIFeatureLayerFieldDefinition> fieldList = new List<ESRIFeatureLayerFieldDefinition>();
fieldList.Add(addField);

Next, I put that list into a dictionary of type string, object, and gave it a key of "fields":

Dictionary<string, object> fieldArray = new Dictionary<string, object>();
fieldArray["fields"] = fieldList;

I created my RestRequest, specifying it as a "POST" method along with the URL to my service:

RestRequest request = new RestRequest(url, Method.POST);
request.Method = Method.POST;

Then I added a parameter for the fields. The only thing is, the service is looking for a parameter called "addToDefinition" that CONTAINS the fields object. (I found this out after using fiddler to analyze the request when I submitted a request through esri's web admin interface).

So, I have only one parameter in my request and it looks like this:

request.AddParameter("addToDefinition", serializer.Serialize(fieldArray), ParameterType.GetOrPost);

The "addToDefinition" is the name of the parameter that the REST service is looking for. I used a serializer to serialize the fieldArray dictionary object to get the fields JSON object as a string. Parameter type was GetOrPost, and since it's only one it will be passed as the body of the request.

Any additional querystrings or parameters "f=pjson", "token=" were all appended to the resource URL for my request object.

Once that request completes, you should see the new field in your service definition.
0 Kudos