By Daniel Du
I am asked how to get the list of maps from MapGuide repository. In this post, I will show you how to do it with code, actually it is possible to get a list of resource with specified type, it can be feature source, layer, map or web layout.
The resources can be retrieved by resource service. Here is the help information about EnumerateResources method:
MgByteReader* MgResourceService::EnumerateResources
(
MgResourceIdentifier * resource,
INT32 depth,
CREFSTRING type
)
Enumerates the resources in the specified repository.
Remarks:
You can enumerate all types or just a selected type. You can also choose what depth in the repository to examine. This method only works on "Library" repository. If you specify a repository that is not supported, this method will throw an MgInvalidRepositoryType exception.
Parameters:
resource
(MgResourceIdentifier) Resource identifier specifying the resource to enumerate. This can be a document or a folder.
If it is a folder, you must include the trailing slash in the identifier.
depth
(int) Recursion depth, relative to the specified resource.
- If the resource is a document, depth must be set to 0.
- If the resource is a folder:
- If the depth is equal to 0, only information about the specified folder is returned.
- If the depth is greater than 0, information about the folder and its descendants up to the specified depth are returned.
- If the depth is -1, information about the folder and all its descendants is returned.
type
(String/string) Type of the resource to be enumerated. (Case sensitive.) SeeMgResourceType for valid types.
Or, this can be set to null, in which case information about all resource types is returned.
Returns:
Returns an MgByteReader object containing a description of the resources in XML format using the ResourceList schema.
The resource type can be specified by member of MgResourceType.
For your information, following the implementation of MgResourceType, it is included in MapGuide WebExtension API, you DO NOT need to implement it yourself.
public class MgResourceType
{
// For flexible weblayout
public const string ApplicationDefinition = "ApplicationDefinition";
public const string DrawingSource = "DrawingSource";
public const string FeatureSource = "FeatureSource";
public const string Folder = "Folder";
public const string LayerDefinition = "LayerDefinition";
public const string LoadProcedure = "LoadProcedure";
// For runtime map
public const string Map = "Map";
// For map definition
public const string MapDefinition = "MapDefinition";
public const string PrintLayout = "PrintLayout";
public const string Selection = "Selection";
public const string SymbolDefinition = "SymbolDefinition";
public const string SymbolLibrary = "SymbolLibrary";
// For basic weblayout
public const string WebLayout = "WebLayout";
public MgResourceType();
}
Following code snippet demonstrates how to get all maps, you make some minior changes to retrieve other type of resources very easily:
/// <summary>
/// enumberate all the map definitions in the
/// </summary>
/// <param name="resId"></param>
/// <returns></returns>
public Dictionary<string, string> GetAllMaps(MgResourceIdentifier resId)
{
Dictionary<string, string> mapList = new Dictionary<string, string>();
MgResourceService resSvc = siteConnection.CreateService
(MgServiceType.ResourceService) as MgResourceService;
// Get all map def resources
// MgResourceType.WebLayout for basic weblayout and
// MgResourceType.ApplicationDefinition for flexible weblayout
MgByteReader byteRdr = resSvc.EnumerateResources(resId, -1,
MgResourceType.MapDefinition);
//Convert to string
String MapResStr = byteRdr.ToString();
//Load into XML document so we can parse and get the names of the maps
XmlDocument doc = new XmlDocument();
doc.LoadXml(MapResStr);
//let's extract the map names and list them
XmlNodeList MapResIdNodeList;
XmlElement root = doc.DocumentElement;
MapResIdNodeList = root.SelectNodes("//ResourceId");
int nRes = MapResIdNodeList.Count;
for (int i = 0; i < nRes; i++)
{
XmlNode MapResIdNode = MapResIdNodeList.Item(i);
String MapRes = MapResIdNode.InnerText;
int index1 = MapRes.LastIndexOf('/') + 1;
int index2 = MapRes.IndexOf("MapDefinition") - 2;
int length = index2 - index1 + 1;
string mapName = MapRes.Substring(index1, length);
if (!mapList.ContainsKey(mapName))
{
mapList.Add(mapName, MapRes);
}
}
return mapList;
}
Hope this helps.