Welcome to ESRI Blogs

Determining layer IDs at runtime

In this post, Rex Hansen provides a tip for determining the ID for a given layer: 

The SearchAttibutesTask and QueryAttributesTask controls are designed to use a layer ID to define the layer within a resource on which a query will execute. When both tasks are configured at design-time the layer ID is stored in the definition of the query. The layer IDs are determined by the order of the layers in a resource (i.e. map service).

The layer order can change when existing layers are reordered or removed, or when new layers are added. In this situation, the layer IDs defined when you configured the SearchAttibutesTask and QueryAttributesTask become invalid. However, if the layer names will remain constant and unique you can discover the layer ID at runtime and update the query definition for both tasks. Although this pattern will add some overhead to each request, the layer ID will be dynamically updated without having to manually change and recompile the web application. The following code provides an example of this technique.

    protected void Page_Load(object sender, EventArgs e)

    {

        ESRI.ArcGIS.ADF.Web.DataSources.IGISFunctionality gisfunctionality = 

            Map1.GetFunctionality("MapResourceItem0");

 

        ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mf = 

            (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)gisfunctionality;

 

        string[] layerids;

        string[] layernames;

        mf.GetLayers(out layerids, out layernames);

 

        for (int i = 0; i < layerids.Length; i++)

        {

            string layername = layernames[i];

            if (layername == "counties")

            {

                SearchAttributesTask1.SearchFields = 

                    string.Format("MapResourceManager1:::MapResourceItem0:::{0}:::NAME", layerids[i]);

                QueryAttributesTask1.PredefinedQuery.LayerID = layerids[i];

            }

        }

    }

Published Tuesday, June 26, 2007 10:16 AM by sterlingdq
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Determining layer IDs at runtime

that's going to work on page load for one layer ... iff 0 is the place of your map.  If you have a graphics layer(s) in your project this code will break.  Identifies add graphics layers also so be careful hard-coding your mapfunctionality index.

Tuesday, June 26, 2007 3:23 PM by sgourley

# Determining layer by name at runtime

Another GIS code bookmark to implement tomorrow. This post is from Rex Hansen, the most helpful software

Wednesday, June 27, 2007 10:12 PM by Al Pascual

# Determining layer IDs at runtime

Great post! I have chosen to do it this way to keep from hardcoding any indexes. Hope this method will help others. publiic System.Collections.Hashtable getLayerIDMatrix(MapResourceManager MapResourceManager, string MapResource) { MapResourceItem mapResourceItem = MapResourceManager.ResourceItems.Find(MapResource); IGISResource igisr = mapResourceItem.Resource.DataSource.Resources.Find(MapResource); string url = ""; if (igisr is MapResourceInternet) { MapResourceInternet mri = (MapResourceInternet)igisr; url = mri.Url; } MapServerProxy msp = new MapServerProxy(url); System.Collections.Hashtable matrix = new System.Collections.Hashtable(); MapServerInfo mapInfo = msp.GetServerInfo(msp.GetDefaultMapName()); foreach (MapLayerInfo mapLayerInfo in mapInfo.MapLayerInfos) { matrix.Add(mapLayerInfo.LayerID, mapLayerInfo.Name); } return matrix; }
Sunday, April 20, 2008 10:01 AM by Justin Cornell

# re: Determining layer IDs at runtime

Is there a similar method for REST?
Thursday, June 25, 2009 9:09 AM by Jonathan Pickus

Leave a Comment

(required) 
required 
(required)