Select to view content in your preferred language

connecting to any arcgis server dynamically

2015
13
05-30-2012 01:09 AM
shaziyashaikh
Emerging Contributor
Hi,

Given any arcgis server url, i want to fetch all its services in the list box.
When i click on any layer from the list box, that layer should be displayed on my map

can anyone pls tell me how to dynamically fetch all services of any arcgis server.

thanks
0 Kudos
13 Replies
JoeHershman
MVP Alum
This sample http://blogs.esri.com/esri/apl/2012/03/06/dynamic-image-service-directory/ builds a directory of Image Services which would be the same just don't filter out based on it being an Image Service.  Basically you query the service catalog and get the json representation and parse that out into the Service names and associated urls.  I myself wrote a rest service that connects from the server side and using ArcObjects gets the inventory and my Silverlight applications calls that service
Thanks,
-Joe
0 Kudos
DominiqueBroux
Esri Frequent Contributor
There is no out of the box SL functions allowing to browse the services of an ArcGIS server.

However this thread http://forums.arcgis.com/threads/5934-View-all-ArcGIS-Server-services-layers-in-a-treeview-control(W...) might give idea on how to do it.
0 Kudos
shaziyashaikh
Emerging Contributor
Thanks for reply,

the link mentioned is not opening.
I tried following code from this link http://forums.esri.com/Thread.asp?c=158&f=2276&t=279387
ESRI.ArcGIS.GISClient.IAGSServerConnectionFactory2 connectionFactory;
            connectionFactory = (ESRI.ArcGIS.GISClient.IAGSServerConnectionFactory2)new ESRI.ArcGIS.GISClient.AGSServerConnectionFactory();
            //create a property set to hold connection properties
            IPropertySet connectionProps;
            connectionProps = new PropertySet();
            //specify the URL for the server
            connectionProps.SetProperty("URL", "http://services.arcgisonline.com/arcgis/services");
            //define username and password for the connection
            //connectionProps.SetProperty("USER", "<USER>");
            //connectionProps.SetProperty("PASSWORD", "<PASS>");
            //open the server connection, pass in the property set, get a connection object back
            ESRI.ArcGIS.GISClient.IAGSServerConnection gisServer;
            gisServer = connectionFactory.Open(connectionProps, this.Handle.ToInt32());

            //get an enum of all server object names from the server (GIS services, i.e.)
            ESRI.ArcGIS.GISClient.IAGSEnumServerObjectName soNames = gisServer.ServerObjectNames;
            ESRI.ArcGIS.GISClient.IAGSServerObjectName3 soName;
            //loop thru all services, find a map service called "I3_Imagery_Prime_World_2D" (high res imagery for the world)
            soName = (ESRI.ArcGIS.GISClient.IAGSServerObjectName3)soNames.Next();
            do
            {
                if ((soName.Type == "MapServer") && (soName.Name == "I3_Imagery_Prime_World_2D"))
                {
                    break;  //found it
                }
                //keep searching the services ...
                soName = (ESRI.ArcGIS.GISClient.IAGSServerObjectName3)soNames.Next();
            } while (soName != null);
            //if the desired service was found ...
            if (soName != null)
            {
                //create a layer factory to make a new MapServerLayer from the server object name
                ILayerFactory msLayerFactory;
                msLayerFactory = new MapServerLayerFactory();
                //create an enum of layers using the name object (will contain only a single layer)
                IEnumLayer enumLyrs = msLayerFactory.Create(soName);
                //get the layer from the enum, store it in a MapServerLayer variable
                IMapServerLayer mapServerLayer;
                mapServerLayer = (IMapServerLayer)enumLyrs.Next();
                //make sure the layer is not empty (Nothing), then add it to the map
                if (mapServerLayer != null)
                {
                    IMap.AddLayer((ILayer)mapServerLayer);
                }
            }

But here many errors appear as i dont have library reference ESRI.ArcGIS.GISClient.

Can you resend me the link in ur earlier reply.
Or check if above code is correct what i am looking for, if yes pls tell me how to i get reference for  ESRI.ArcGIS.GISClient

thanks
0 Kudos
JoeHershman
MVP Alum
That is ArcObjects it will not work in a Silverlight application.
Thanks,
-Joe
0 Kudos
shaziyashaikh
Emerging Contributor
So can you pls suggest me what should I go for? Is there any alternate method in silverlight other than using ArcObjects.
0 Kudos
JoeHershman
MVP Alum
I am not sure about how

dbroux

application works but I would imagine it has to be similar to what the link I posted for the Imagery application.  The imagery application does exactly what you want except it filters to only display Image Services in the tree (basically one conditional statement that would need to be removed). 


Get the catalog data from the server rest endpoint.  If you put you services url (http://SERVER/arcgis/rest/services?f=json) you can see the json string try  (http://SERVER/arcgis/rest/services?f=pjson) for something easier to read.  The json can be converted to objects and the catalog built from there.  Which is done in the application link I posted.
Thanks,
-Joe
0 Kudos
DominiqueBroux
Esri Frequent Contributor
You can't use ArcObjects at the client side of a silverlight application.
You can do it at the server side by creating a SOE (Server Object Extension) but I don't recommend that approach in your case.

Both referenced samples (Joe's one and mine) are using REST to know the folders and the services.

Basically you have to deserialize server answers such as :
0 Kudos
shaziyashaikh
Emerging Contributor
Hi,

Thanks very much for giving response. It really helped me.

Now I am getting error in MapService.cs file
There was an error deserializing the object of type ESRI.ArcGIS.ServerExplorer.MapServiceInfo. Data at the root level is invalid. Line 1, position 1.

at the code   this.m_mapServiceInfo = dataContractJsonSerializer.ReadObject(memoryStream) as MapServiceInfo;

can you please tell me why this is throwing such error at runtime

thanks
0 Kudos
DominiqueBroux
Esri Frequent Contributor
The sample is quite old and migh have to be updated for new services or for errors returned by the services.

In your case, it's difficult to analyze without knowing the json string returned by the service.

I suggest you use either fiddler or the debugger to look at the json string which is badly deserialized. That might give a clue.
0 Kudos