GeoServer OpenLayers – How to Get Names of All Layers in a Store in GeoServer Using OpenLayers

geoserverlayersopenlayersows

I have strict requirement to retrieve names of all layers in a GeoServer store using OpenLayers. I've gone through the post here, but it doesn't answer my question. I've also gone through GeoServer's REST API, which would be ideal if I'm using another language, say C#, Java, or PHP. But I want to do this entirely in JavaScript.

UPDATE:
From what I can see in the WFS GetCapabilities document, I can retrieve names of all layers by sending a WFS GetCapabilities request using filters. But I'm not sure if its possible to incorporate filters in a GetCapabilities request. Neither do I have any idea how such a filter will look like.

Any advice on how I can go about achieving this will be highly appreciated. Any alternative approach will also be appreciated.

UPDATE:

I believe I've found a solution here; "WFS GetCapabilities requests can be filtered to only return layers corresponding to a particular namespace. To do this, add the following code to your request: namespace=<namespace>". I hope this assists someone else.

Best Answer

I notice you have already found a solution that works for you, but I thought I might add some additional options that you or others may find useful.

In GeoServer there is a capability known as Virtual OWS Services. The WMS, WFS, and WCS services are collectively known as the OWS services. When you make a request to one of these services you are making a global request, so all registered layers are returned in the capabilities document (unless you have data security settings). By contrast, a virtual service is a filtered view of the global service. The filtering is done on workspaces and is accessed through a slightly different url.

Lets take an example. Suppose you have a workspace called myws and it is this workspace that you would like to list layers for. In that case you could make a request to the url:

http://www.yourgeoserver.com/geoserver/myws/ows?SERVICE=WFS&REQUEST=GetCapabilities

The returned capabilities document will only list the layers registered against the myws workspace.

That is one approach, however you had originally asked about accessing the layers list through JavaScript. Happily there is a way to do this using the GeoServer REST API. You can get a list of featuretypes for a given workspace and datastore using something like:

http://www.yourgeoserver.com/geoserver/rest/workspaces/myws/featuretypes.json

This will return you a JSON object listing all of the featuretypes within the workspace, you can also substitute .json for .xml or .html to get the response in those formats. So, how to do this in OpenLayers? Fortunately OpenLayers provides an encapsulation of the XMLHttpRequest object in the form of OpenLayers.Request object. A simple example using the REST url above would be:

var request = OpenLayers.Request.GET({
  url: "http://www.yourgeoserver.com/geoserver/rest/workspaces/myws/featuretypes.json",
  callback: function(request) {
    // Code here to handle the response, the request object contains the data
  }
});

There is a good document on requesting remote data with OpenLayers here.

Hope that gives you some other approaches to consider.