Select to view content in your preferred language

how to query map knowing map layer name (not id)

5613
16
11-15-2010 10:50 AM
ThaoNguyen
Emerging Contributor
HI,
I have a map layer name, but don't have map id.  How can I write the QueryTask that is needed to query the map?  How to get layer id if I know map layer name?  Please help!  Thank you so much!!

Below is my code:

QueryTask queryTask = new QueryTask(MapURL); //I don't know the layer ID, therefore, cannot do MapURL + "/<layerid>")
                queryTask.Failed += QueryTask_Failed;
                queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
              
                // Specify fields to return from query
                Query query = new ESRI.ArcGIS.Client.Tasks.Query();
                query.OutFields.Add("*");
                query.Where = "0=0";

                // Return geometry with result features
                query.ReturnGeometry = true;
                queryTask.ExecuteAsync(query);
16 Replies
DominiqueBroux
Esri Frequent Contributor
 
<esri:ArcGISDynamicMapServiceLayer x:Uid="dynMapSvcId" ID="BigMap" Url="" />
 


My understanding is that you know the url of your map service, so it should be:
 
<esri:ArcGISDynamicMapServiceLayer x:Uid="dynMapSvcId" ID="BigMap" Url="http://<server>/ArcGIS/rest/services/<mapService>" />


then you can add an Initialized event handler:
 
<esri:ArcGISDynamicMapServiceLayer x:Uid="dynMapSvcId" ID="BigMap" Url="http://<server>/ArcGIS/rest/services/<mapService>" 
Initialized="BigMap_Initialized" />


and in the event handler you can use the LayerInfo array to define which layerID you want to work with and make your query.

Note that instead of using a graphicslayer and a query, you could use a feature layer which is basically doing that without any code from you. You just have to initialize the Url of the feature layer which is also something like http://<server>/ArcGIS/rest/services/<mapService>/LayerID
0 Kudos
JenniferNery
Esri Regular Contributor
How about initializing the ArcGISDynamicMapServiceLayer in code, subscribe to its Initialized event, call Initialize(). At it's Initialized event handler, retrieve the sublayer Name and ID information. Save this info onto a dictionary with Name as Key and ID as value.

Once you are given the sub layer name, you can get the corresponding ID from this dictionary and append it to the ArcGISDynamicMapServiceLayer URL so you can run your query against it.

The ArcGISDynamicMapServiceLayer in this case is not added to your map but is Initialized and used to get its sub layers.
0 Kudos
ThaoNguyen
Emerging Contributor
 
<esri:ArcGISDynamicMapServiceLayer x:Uid="dynMapSvcId" ID="BigMap" Url="" />
 


My understanding is that you know the url of your map service, so it should be:
 
<esri:ArcGISDynamicMapServiceLayer x:Uid="dynMapSvcId" ID="BigMap" Url="http://<server>/ArcGIS/rest/services/<mapService>" />


then you can add an Initialized event handler:
 
<esri:ArcGISDynamicMapServiceLayer x:Uid="dynMapSvcId" ID="BigMap" Url="http://<server>/ArcGIS/rest/services/<mapService>" 
Initialized="BigMap_Initialized" />


and in the event handler you can use the LayerInfo array to define which layerID you want to work with and make your query.

Note that instead of using a graphicslayer and a query, you could use a feature layer which is basically doing that without any code from you. You just have to initialize the Url of the feature layer which is also something like http://<server>/ArcGIS/rest/services/<mapService>/LayerID


The problem I have now is I do not have layerID (which is a number); but I have a layer name.  I do not know map service until someone passes it to me; therefore, I want to put it in code. In addition, it is changed...  Anyway, that is not important, what I want is I want to run a queryTask that I can pass in layer name, not layer id which I do not have.
0 Kudos
DaveTimmins
Deactivated User
Hi,

something like this?

int GetLayerID(string layerName)
        {
            var client = new WebClient();
            client.DownloadStringCompleted += (sender, args) =>
            {
                string json = args.Result;
                foreach (var layer in JObject.Parse(json)["layers"].Children())
                {
                    if (string.Equals((string)layer["name"], layerName, StringComparison.OrdinalIgnoreCase))
                        return (int)layer["id"];
                }
                throw new InvalidOperationException(string.Format("A layer with the name '{0}' was not found in the MXD.", layerName));
            };
            string uri = "your map server url" + "?f=json";
            client.DownloadStringAsync(new Uri(uri));
        }
0 Kudos
ThaoNguyen
Emerging Contributor
Hi,

something like this?

int GetLayerID(string layerName)
        {
            var client = new WebClient();
            client.DownloadStringCompleted += (sender, args) =>
            {
                string json = args.Result;
                foreach (var layer in JObject.Parse(json)["layers"].Children())
                {
                    if (string.Equals((string)layer["name"], layerName, StringComparison.OrdinalIgnoreCase))
                        return (int)layer["id"];
                }
                throw new InvalidOperationException(string.Format("A layer with the name '{0}' was not found in the MXD.", layerName));
            };
            string uri = "your map server url" + "?f=json";
            client.DownloadStringAsync(new Uri(uri));
        }


Thank you so much!! That is what I'm looking for. 
BTW, is the space name for JObject Newtonsoft.Json.Linq?  Just want to double check before I add the dll that contains this object to my VStudio project.
0 Kudos
DaveTimmins
Deactivated User
Yes that's correct. You'll need the Newtonsoft.Json.Silverlight library.
0 Kudos
ThaoNguyen
Emerging Contributor
Hi,

something like this?

int GetLayerID(string layerName)
        {
            var client = new WebClient();
            client.DownloadStringCompleted += (sender, args) =>
            {
                string json = args.Result;
                foreach (var layer in JObject.Parse(json)["layers"].Children())
                {
                    if (string.Equals((string)layer["name"], layerName, StringComparison.OrdinalIgnoreCase))
                        return (int)layer["id"];
                }
                throw new InvalidOperationException(string.Format("A layer with the name '{0}' was not found in the MXD.", layerName));
            };
            string uri = "your map server url" + "?f=json";
            client.DownloadStringAsync(new Uri(uri));
        }


For those who are interested, another way to get the json (without waiting for the event to be completed)

var client = new System.Net.WebClient();
try
{
using (StreamReader reader = new StreamReader(client.OpenRead("your map url here" + "?f=json")))
string json = reader.ReadToEnd();

//do get layer Id like Dave posted
foreach (var layer in JObject.Parse(json)["layers"].Children())
                {
                    if (string.Equals((string)layer["name"], layerName, StringComparison.OrdinalIgnoreCase))
                        return (int)layer["id"];
                }

}
catch (Exception ex)
{
//your code here
}
0 Kudos