How to get names of all layers in a store in GeoServer using OpenLayers

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.


This answer might help someone. I wanted to build a list of my layers automatically using Javascript and Leaflet. Using CHenderson answer, I made the following code:



    url = "https://YOUSERVER/geoserver/STORE/ows?service=wfs&version=2.0.0&request=GetCapabilities"
    layerList = [];
    $.ajax({
        type: "GET",
        url: url,
        dataType: "xml",
        success: function(xml) {
            $(xml).find('FeatureType').each(function(){
                name = $(this).find("Name").text();
                title = $(this).find("Title").text();
                group = "NO_GROUP";
                $(this).find('ows\\:Keywords').each(function(){
                    keyword = $(this).find('ows\\:Keyword').text();
                    if(keyword.indexOf("group:")!=-1)
                    {
                        group=keyword.split(":")[1];
                        return false;
                    }
                });
                layerList.push({"name":name,"title":title,"group":group});          
            });
        }
    });

This way, I can add a keyword "group:GEOGRAPHY"enter image description here on the Geoserver layer and I don´t have to manually code each layer and its group. The name is used to create the layer (maybe using L.WMS.source) and the title is the html label.