arcobjectsarcgis-serverarcmap

How to create an ArcMap Layer from a ArcGIS Map Service


I would like to add an ILayer created from an ArcGIS Server Map service to an IMap with ArcObjects, but don't see how to do it.

I am getting an IMapServer3 with the following code, where mapName = the map service:

serverContext = som.CreateServerContext(mapName, "MapServer");
IServerObject serverObject = serverContext.ServerObject;
IMapServer3 mapServer = (IMapServer3)serverObject;

It looks like I can get an ILayer from an IMapServerGroupLayer, but it looks like the IMapServerGroupLayer is looking for a different connection type than I am using.

If you have an example of getting an ILayer from a Map Service, your assistance is appreciated.


Solution

  • This is what worked...

    private static void GetLayerFromMapServerLayer()
    {
    
    IAGSServerConnectionName servConnName = new AGSServerConnectionNameClass();
    IPropertySet props = new PropertySetClass();
    props.SetProperty("machine", "server");
    servConnName.ConnectionProperties = props;
    
    
    
    IAGSServerConnectionFactory pAGSServerConnectionFactory = new AGSServerConnectionFactoryClass();
    IAGSServerConnection pAGSConnection = pAGSServerConnectionFactory.Open(props, 0);
    
    
    
    IAGSEnumServerObjectName pEnumSOName = pAGSConnection.ServerObjectNames;
    
    IAGSServerObjectName pSOName = pEnumSOName.Next();
    
    while (pSOName != null)
    {
    if (pSOName.Name == "Base_Map")
    break;
    pSOName = pEnumSOName.Next();
    }
    
    IName pName = (IName)pSOName;
    IMapServer mapServer = (IMapServer)pName.Open();
    
    IMapServerLayer msLyr = new MapServerLayerClass();
    msLyr.ServerConnect(pSOName, mapServer.DefaultMapName);
    
    IMapServerGroupLayer group = (IMapServerGroupLayer)msLyr;
    
    ILayer msLayer = (ILayer)msLyr;
    
    //return msLayer;
    MapDocument mapDoc = new MapDocumentClass();
    mapDoc.Open(@"F:\~mkoneya~2011_82_13_58_30.mxd");
    IMap myMap = mapDoc.get_Map(0);
    myMap.AddLayer(msLayer);
    mapDoc.Save();
    }