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 = 



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



        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];




This entry was posted in Services and tagged , , , . Bookmark the permalink.

Leave a Reply

One Comment

  1. sgourley says:

    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.