Welcome to ESRI Blogs

Creating a utility library (Part II): Creating and adding resources

Morten Nielsen, a developer on the .NET Web ADF team, contributed this series:

This post is the second in a series on creating a set of reusable helper methods that tries to make some of the common developer tasks when working with the .NET Web ADF easier. This specific article shows how to create a few static methods that you can use to create, add and initialize resources to your map.

The simplest way of creating resources is by using the resource definition string. If you use the designer in Visual Studio and add a resource to the MapResourceManager, then switch to source view, you can see what the resource definition string looks like:

<esri:MapResourceManager ID="MapResourceManager1" runat="server">
<ResourceItems>
<esri:MapResourceItem Definition="<Definition DataSourceDefinition="http://myserver/arcgis/services" DataSourceType="ArcGIS Server Internet" Identity="" ResourceDefinition="(default)@Counties" DataSourceShared="True" />"
DisplaySettings="visible=True:transparency=0:mime=True:imgFormat=PNG8:height=100:width=100:dpi=96:color=:transbg=False:displayInToc=True:dynamicTiling="
Name="MapResourceItem0" />
</ResourceItems>
</esri:MapResourceManager>

The definition string has been encoded to remove quotes and < and > characters, so the "real" definition string for the above actually reads:

<Definition DataSourceDefinition="http://myserver/arcgis/services" DataSourceType="ArcGIS Server Internet" Identity"" ResourceDefinition="(default)@Counties" DataSourceShared="True">

We can create a resource based on this string using the constructor on GISResourceItemDefinition:

MapResourceItem mri = new MapResourceItem();
mri.Definition = new GISResourceItemDefinition(definition);

Let’s take this concept further and create two helper methods for inserting and adding resources to the MapResourceManager:

public static class Common
{
public static MapResourceItem AddResource(string definition, string name, MapResourceManager mapResourceManager)
{
return InsertResource(definition, name, mapResourceManager, mapResourceManager.ResourceItems.Count);
}
public static MapResourceItem InsertResource(string definition, string name, MapResourceManager mapResourceManager, int index)
{
//Ensure unique name by appending a number to it if it already exists
string uniqueName = name;
int counter = 1;
while (mapResourceManager.ResourceItems.Find(uniqueName) != null)
{
uniqueName = String.Format("{0}_{1}", name, ++counter);
}
//Create resource
MapResourceItem mri = new MapResourceItem();
mri.Definition = new GISResourceItemDefinition(definition);
mri.Name = name;
//Add and initialize resource
mapResourceManager.ResourceItems.Insert(index, mri);
IMapResource mr = mapResourceManager.CreateResource(mri);
mapResourceManager.Initialize(mri);
return mri;
}
}

The library that you can download takes this helper method a little further. Instead of using the MapResourceManager as a parameter, it takes the map instead. The method will take care of zooming to the first resource you add, return error messages if the resource was not available, etc. I also added several overloads to give you more fine-grained control of the resources you added.

So this gives us an easy way of adding any kind of map resource to the Map. By analyzing the definition string we can also create some data source-specific methods that will create the definition strings based on for instance the server endpoint and service name. Here are a few for ArcGIS Server Internet, ArcGIS Server Local and ArcIMS:

public static class ArcGISServer
{
public static MapResourceItem AddLocalResource(string servername, string servicename, MapResourceManager mapResourceManager)
{
return AddLocalResource(servername, servicename, mapResourceManager, string.Empty);
}
public static MapResourceItem AddLocalResource(string servername, string servicename, MapResourceManager mapResourceManager, string identity)
{
string definition = string.Format("<Definition DataSourceDefinition=\"{0}\" DataSourceType=\"ArcGIS Server Local\" Identity=\"{1}\" ResourceDefinition=\"(default)@{2}\" DataSourceShared=\"True\" />",
servername, identity, servicename);

return Common.AddResource(definition, servicename, mapResourceManager);
}
public static MapResourceItem AddHttpResource(string serverURI, string servicename, MapResourceManager mapResourceManager)
{
return AddHttpResource(serverURI, servicename, mapResourceManager, string.Empty);
}
public static MapResourceItem AddHttpResource(string serverURI, string servicename, MapResourceManager mapResourceManager, string identity)
{
string definition = string.Format("<Definition DataSourceDefinition=\"{0}\" DataSourceType=\"ArcGIS Server Internet\" Identity=\"{1}\" ResourceDefinition=\"(default)@{2}\" DataSourceShared=\"True\" />",
serverURI, identity, servicename);

return Common.AddResource(definition, servicename, mapResourceManager);
}
}

public static class ArcIMS
{
public static MapResourceItem AddResource(string servername, int port, string servicename, MapResourceManager mapResourceManager)
{
string definition = string.Format("<Definition DataSourceDefinition=\"{0}@{1}\" DataSourceType=\"ArcIMS\" Identity=\"\" ResourceDefinition=\"(default){2}\" DataSourceShared=\"True\" />",
servername, port, servicename);

return Common.AddResource(definition, servicename, mapResourceManager);
}
}

If we combine this with the previous post on how to simplify callback results, we can easily create a callback button that adds a resource, updates the table of contents etc.

protected void CallbackButton1_Clicked(object sender, EventArgs args)
{
//Add ArcGIS Server resource
ArcGISServer.AddHttpResource("http://myserver/arcgis/services", "Counties",
Map1);

Toc1.Refresh(); //Refresh TOC

//Disable the CallbackButton so user cannot click the button again
CallbackButton1.CallbackResults.Add(
Callbacks.CreateJavaScript(
String.Format("document.getElementById('{0}').disabled = -1;",
CallbackButton1.ClientID)));

//Ensure CallbackResults from all controls are returned to the client
WebADF.Utilities.Callbacks.AutoMoveCallbackResults();
}

Download the Web ADF Utility Class

Update: The utility library has since been updated to support 9.3 partial postbacks, and can be found in the Code Gallery.

Published Friday, February 22, 2008 10:42 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: Creating a utility library (Part II): Creating and adding resources

This is great stuff...how can I remove a resource item? I tried changing the Common class, but can't get it to work. Thanks.
Friday, April 04, 2008 6:22 PM by Pablo

# re: Creating a utility library (Part II): Creating and adding resources

This is great stuff...is there a way to remove a resource as well? I've been trying to modify the Common class, but doesn't seem to work? Thanks.
Friday, April 04, 2008 6:25 PM by Pablo

# re: Creating a utility library (Part II): Creating and adding resources

I figured it out, below is the code if anybody wants it. protected void CallbackButton5_Clicked(object sendedr, EventArgs args) { GISResourceItemCollection mri = MapResourceManager1.ResourceItems; MapResourceItem mapResourceInfo = new MapResourceItem(); // Remove resource mapResourceInfo = mri.Find("DAMO_BaseMap"); mri.Remove(mapResourceInfo); UpdateAfterResourceChanged(); }
Wednesday, April 09, 2008 9:47 AM by Pablo

# re: Creating a utility library (Part II): Creating and adding resources

To remove the resource remember the name you gave it (see below) and use: GISResourceItemCollection mapResourceItemCollection = MapResourceManager1.ResourceItems; MapResourceItem item = mapResourceItemCollection.Find(myuniquename); if (item != null) { mapResourceItemCollection.Remove(item); } For adding AGS internet-services I use the documentation at: http://edndoc.esri.com/arcobjects/9.2/NET_Server_Doc/developer/ADF/resources.htm I say something like this to remember the name: string myuniquename = servicename + "." servername; mapResourceItem.Name = myuniquename; Best regards Axel
Friday, April 11, 2008 4:33 AM by AxxL

Leave a Comment

(required) 
required 
(required)