Select to view content in your preferred language

How to get attribute columns list

1108
6
11-25-2010 04:55 AM
PaulHuppé
Deactivated User
Hi,
For a given map service and layer, is it possible to get the list of attribute columns?  If so, how?
Thanks,
Paul
0 Kudos
6 Replies
ChrisSmith
Emerging Contributor
just call your service via rest for the required layer i.e.
http://servername/ArcGIS/rest/services/NameOfYourService/MapServer/0?f=json

(0 = the layer you want to get info on, change this number to the index of the required layer.)

this will then return the layer description in the specified format and you just need to deserialise or parse the json (in this instance) to get the fields; i.e. look for the following...

"fields":[{"name":"FID","type":"esriFieldTypeOID","alias":"FID"}................

regards

chris
0 Kudos
PaulHuppé
Deactivated User
Hi Chris,

Thanks for the information.  How do you do that from code behind?
Cheers,
Paul
0 Kudos
ChrisSmith
Emerging Contributor
try using WebClient

WebClient client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(this.Client_DownloadStringCompleted);
                string configUrl = string.Format("{0}{1}", this.Url, "?f=json");
                client.DownloadStringAsync(new Uri(configUrl));  


then deserialise the json result.

public static T DeserializeJson<T>(string jsonString)
        {
            using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
            {
                try
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                    T returnObject = (T)serializer.ReadObject(memoryStream);
                    return returnObject;
                }
                catch (Exception e)
                {
                    ////Logging.Log.Error("Deserialize json string");
                    return default(T);
                }
            }
        }


you will need to set up a class that defines the DataContract in the returned json, below is an example for the whole service (MapServer part of url without the layer index)....

using System.Runtime.Serialization;

namespace Your.Namespace
{
    [DataContract]
    public class DocumentInfo
    {
        [DataMember]
        public string Title { get; set; }
        [DataMember]
        public string Author { get; set; }
        [DataMember]
        public string Comments { get; set; }
        [DataMember]
        public string Subject { get; set; }
        [DataMember]
        public string Category { get; set; }
        [DataMember]
        public string Keywords { get; set; }
        [DataMember]
        public string Credits { get; set; }
    }

    [DataContract]
    public class ExtentConfig
    {
        [DataMember]
        public double? xmin { get; set; }.....

[DataContract]
    public class ServiceConfig
    {
        [DataMember]
        public DocumentInfo documentInfo { get; set; }

        [DataMember]
        public string serviceDescription { get; set; }

        [DataMember]
        public string mapName { get; set; }

        [DataMember]
        public string description { get; set; }

        [DataMember]
        public string copyrightText { get; set; }....


to call the desrialiser would be

ServiceConfig deserialisedConfig = Serializer.DeserializeJson<ServiceConfig>(e.Result);


where e.Result is the DownloadStringCompletedEventArgs parameter of your DownloadStringCompletedEventHandler on the WebClient set at the beginning

should be enough for you to work out how to do it, there are other ways of doing it

hope this helps

regards

chris
0 Kudos
PaulHuppé
Deactivated User
Hi Chris,

Ok, I will give it a try.
thanks,
Paul
0 Kudos
PaulHuppé
Deactivated User
I have added a reference to the System.Runtime.Serialization.Json namespace, but the DataContractJsonSerializer Class is not present. I am using Silverlight 4 and the .Net 4 framework, so I do not know why it is not there!
0 Kudos
DominiqueBroux
Esri Frequent Contributor
The class DataContractJsonSerializer is in System.ServiceModel.Web.
0 Kudos