Add map service to ArcMap

3014
1
11-25-2011 01:44 PM
Highlighted
Occasional Contributor II
Can someone provide a code snippet to add a map service to ArcMap?

In addition, the map service contains many layers.  I just want to add a few of the many layers. 

How can I do this?

Thanks!
Reply
0 Kudos
1 Reply
Highlighted
by Esri Contributor
Esri Contributor

The code sample below shows how one can add a map service to ArcMap using the GxCatalog dialog.

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using ESRI.ArcGIS.ArcMapUI;

using ESRI.ArcGIS.CatalogUI;

using ESRI.ArcGIS.Catalog;

using ESRI.ArcGIS.Carto;

using ESRI.ArcGIS.Geodatabase;

using System.Windows.Forms;

using ESRI.ArcGIS.GISClient;

using ESRI.ArcGIS.esriSystem;

namespace GxDialog_Select_MapService_Layer

{

      public class GxDialog_Select_MapService_Layer : ESRI.ArcGIS.Desktop.AddIns.Button

      {

          public GxDialog_Select_MapService_Layer()

          {

          }

          protected override void OnClick()

          {

              IMxDocument pmxdoc = (IMxDocument)ArcMap.Application.Document;

              IMap pmap = pmxdoc.FocusMap;

              IGxDialog pGxDialog = new GxDialog();

         

               pGxDialog.Title  =  "Browse Data";

              //pGxDialog.set_StartingLocation("C:\\Temp");

               pGxDialog.set_StartingLocation("GIS Servers");

              IEnumGxObject pEnumGx;

              if (!pGxDialog.DoModalOpen(0, out pEnumGx))

                   return; // Exit if user presses Cancel

              IGxObject pgxobject = pEnumGx.Next();

              IGxAGSObject gxAGSObject = pgxobject as IGxAGSObject;

              IAGSServerObjectName agsServerObjectName = gxAGSObject.AGSServerObjectName;

              IAGSServerConnectionName agsServerConnectionName =

                            agsServerObjectName.AGSServerConnectionName;

              IPropertySet propertySet = agsServerConnectionName.ConnectionProperties;

              //create a new ArcGIS Server connection factory

              IAGSServerConnectionFactory2 agsServerConnectionFactory2 =

                             (IAGSServerConnectionFactory2)new AGSServerConnectionFactory();

              IAGSServerConnection agsServerConnection =

                              agsServerConnectionFactory2.Open(propertySet, 0);

               //get an enum of all server object names (services) from the server

              IAGSEnumServerObjectName soNames = agsServerConnection.ServerObjectNames;

              IAGSServerObjectName3 soName = (IAGSServerObjectName3)soNames.Next();

              ILayerFactory msLayerFactory = new MapServerLayerFactory();

              IEnumLayer enumLyrs = msLayerFactory.Create(soName);

              IMapServerLayer mapServerLayer = (IMapServerLayer)enumLyrs.Next();

              pmap.AddLayer(( ILayer)mapServerLayer);

              ArcMap.Application.CurrentTool = null;

          }

    }

}

Reply
0 Kudos