Vishful thinking…

Adding a map resource dynamically to the map using the .NET ADF

Posted in ArcGIS, ESRI by viswaug on April 1, 2008

I just completed a feature in our current project allowing the user to be able to select the map resource to view from a list box and have the map loaded with the selected resource. To accomplish this, we had to add map resources to the map dynamically without it being preset on the MapResourceManager using the Visual Studio designer. Here is a code snippet to insert a “ArcGIS Server Local” map resource into the map.

private MapResourceItem AddMapResource(Map map, AGSLocalMapService mapService, int index)


            MapResourceManager mrm = map.MapResourceManagerInstance;

            MapResourceItem mri = new MapResourceItem();

            GISResourceItemDefinition definition = new GISResourceItemDefinition();

            mri.Name = mapService.Name;

            definition.DataSourceDefinition = mapService.MapServerName;

            definition.DataSourceType = “ArcGIS Server Local”;

            definition.ResourceDefinition = mapService.DataFrame + “@” + mapService.MapService;

            definition.DataSourceShared = true;

            mri.Parent = mrm;

            mri.Definition = definition;

            DisplaySettings displaySettings = new DisplaySettings();

            displaySettings.DisplayInTableOfContents = true;

            displaySettings.Visible = true;

            displaySettings.ImageDescriptor.ImageFormat = ImageFormat.PNG24;

            mri.DisplaySettings = displaySettings;


            mrm.ResourceItems.Insert(index, mri);



            return mri;


and the snippet to insert a graphics resource item.

public static MapResourceItem AddGraphicsMapResource(Map map, string graphicsResourceName)


            if (map != null)


                MapResourceItem mapResourceItem = new MapResourceItem();

                GISResourceItemDefinition graphicsDef = new GISResourceItemDefinition();

                graphicsDef.DataSourceDefinition = “In Memory”;

                graphicsDef.DataSourceType = “GraphicsLayer”;

                mapResourceItem.Definition = graphicsDef;

                DisplaySettings graphicsDisplay = new DisplaySettings();

                graphicsDisplay.DisplayInTableOfContents = false;

                //graphicsDisplay.Transparency = 50.0F;

                graphicsDisplay.Visible = true;

                mapResourceItem.DisplaySettings = graphicsDisplay;

                mapResourceItem.Name = graphicsResourceName;


                map.MapResourceManagerInstance.ResourceItems.Insert(0, mapResourceItem);


                return mapResourceItem;


            return null;



12 Responses

Subscribe to comments with RSS.

  1. Mike said, on April 1, 2008 at 2:07 pm

    A quick note on this, at 9.2 SP3 (not sure about later SPs or 9.3, haven’t had the opportunity to test it out yet) – adding cached resources dynamically behaves differently than when adding them at design time.

    If you add the cached layer dynamically, it will use the cached tiles from disk, but treat it as though it were a dynamic service. So instead of sending direct tiles from disk @ Service_Name/layers/_allLayers/L##/R######/C######.jpg, it first processes them like it does a dynamic image and serves the result from http://host/applcation/ESRI.ArcGIS.ADF.Web.MimeImage.ashx?ImgID={GUID} This re-rendering of the cache tile will output it into whatever format is set in your displaySettings.ImageDescriptor.ImageFormat above

  2. viswaug said, on April 4, 2008 at 4:14 am

    Hi Mike,

    Have you checked the “UseMimeData” property on the map control?

    Thank You,

  3. Greg Cermak said, on May 21, 2008 at 7:10 pm


    I also have to add a map resource dynamically to the map using the .NET ADF for my project. Thanks for the code. I am curious about what type of listbox you used (a listbox that comes with the ESRI toolbar, or just a simple ASP .NET list box)? Are you querying for the available ArcGIS Server services with something like this…

    private void button1_Click(object sender, EventArgs e)
    string endpoint = “http://localhost/arcgis/services”;

    ESRI.ArcGIS.ADF.ArcGISServer.Catalog catalog =
    new ESRI.ArcGIS.ADF.ArcGISServer.Catalog(endpoint);

    ServiceDescription[] sds = catalog.GetServiceDescriptions();

    foreach (ServiceDescription sd in sds)
    System.Diagnostics.Debug.WriteLine(“Service Name: ” + sd.Name);
    System.Diagnostics.Debug.WriteLine(“Service Type: ” + sd.Type);
    System.Diagnostics.Debug.WriteLine(“Service Parent Type: ” + sd.ParentType);
    System.Diagnostics.Debug.WriteLine(“Service Name: ” + sd.Url);
    System.Diagnostics.Debug.WriteLine(“Capabilities: ” + sd.Capabilities);




  4. viswaug said, on May 21, 2008 at 11:27 pm

    Hi Greg,

    I had built an ASP.NET web control with the drop down list to switch map services. I don’t get the map services list from by querying ArcGIS Server, but I have all the connection details for all map services in the web.config file.

    Thank You,

  5. Dainius Bekeris said, on May 22, 2008 at 6:58 am

    Hello Vish.

    We are also adding map resources dynamically. That works fine, but we have problems with removing map resources. Maybe you can give some advice how to handle following situation:

    We add non-pooled map resource dynamically. Non-pooled because we need to editing for this resource. We want that after we remove this resource from MapResourceManager instance of IMapServer is released. If we just remove resource ArcGIS manager shows that map service instance is still in use.

    We tried to call ReleaseContext on server context available in MapResourceLocal, but then another problem: instances for all map resources are released and map refresh is not working (browser hangs waiting for something).

    Same happens if we find server context in session and release it from there.

    Thank you in advance,

    • Kiran said, on June 15, 2009 at 1:55 pm

      Hi Dainius,
      This is Kiran from India, we have same kind of problem. If you get solution can you please send it to me.

      Thanks in Advance,

  6. Danny Hillw said, on September 21, 2008 at 8:45 pm

    Hi Vish,

    I want to enable the users change maps on the go,(code is basically the same at your code), I let the user select a mapresource, and then before inserting a new resource i clear the current mapresourceitems like this:
    wich works fine
    then i add the resources with AddMapResource()… that works fine too.
    but when i try to assign the newly added resource to the overviewmap the problems start, cause it seems that the added resource isn’t quite as initialized as a resourceItem added on design time.
    I have even tried to create a new instance of an overviewmap but that also didn’t work. (it’s the same problem for the magnifying glass)
    The really weird part is that if i insert the map in the create child controls section of my code it kinda works… sometimes and only after hitting f5 (even though all the controls did update…)

    Have you experienced any problems with this?
    Or more importantly do you have a solution or any ideas that might point me in the right direction?

    • Utkarsh Srivastava said, on May 18, 2009 at 10:11 am

      Hi Danny,

      I’m stuck in the exact same situation you have mentioned… I too wanted the users change the map on the go, and the code is the same as that of vish’s. I first clear the map reourceitem just as you’ve mentioned, and My AddResource() works fine…
      but My overviewMap does not gets initialized, even after trying out with a new instance. Also the Zoom to full extent button stops working, along with the glass magnifier. (Zoom to full Extent works only for the Initial map and rest two dont work even for that)

      I tried explicitely assigning Full extent properties of the resource to the mapResourceManager, but somehow it doesnt work. Upon being loaded, the new map is displayed with the zoom and the extent of the last resource.

      I also tried the same after not initializing the MapResourceManager at design time- then whichever is the first service, that gets loaded fine, but after changing the map, the same problem re-appears. Also, Even then, the glass magnifier, Zoom to full extent, and Overview does not work from the very beggining.

      did you find any solution to your problem??? Looking for Some urgent guidance here…

  7. sergio g said, on February 6, 2009 at 4:43 pm

    Hi, I’m trying to do something similar (Im using a web adf .net template in VS2008):

    – Some resource items (based on running services) should be added to the mapResourceManager when the session starts (there is no need for resources to be added, removed or changed later).
    – I guess this should be done in the session_start method. Am I right??
    – If so, how can i access the mapResourceManager object from the session_start method (which is in global.asax)?

    Thanks in advance

  8. Simba said, on March 16, 2009 at 12:30 pm

    Hi I’m a trainee GIS software engineer for giscoe, in pretoria south africa. I am teaching myself the basics of spatial software development using the .NET web ADF specifically using C# to manipulate and work with spatial data. I am sort of going around in circles trying to figure out how to add a new feature to a geodatabase at runtime for instance if i draw a point or polygon or polyline on the map at runtime, how do i make a permanent feature of that map. Please if you could show the whole process and explanation of what happens at each stage i would greatly appreciate it.

  9. GisDummy said, on March 19, 2009 at 6:28 am


    can we publish a GIS Resourse programmatically?

  10. smiley said, on August 26, 2009 at 5:00 pm


    What is the type or namespace for AGSLocalMapService??


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: