Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've the function below which creates or replaces an existing layer group:

public bool AddToLayerGroup(string layerGroupName, string layerName)
                // Retrive the layer group
                string cLayerGroup = GetLayerGroup(layerGroupName);
                // Add the new layer as a new layer node among the retrieved layer nodes
                XmlDocument newLayerGrpXml = AddLayerNodeToLayerGroupXml(layerName, cLayerGroup);
                // Convert the newly created layer group xml to string
                string newLayerGrpString = ConvertXmlToString(newLayerGrpXml);
                newLayerGrpString = newLayerGrpString.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");

                // Delete the existing layer group

                // Now, do ur thing here
                string gUrl = GEOSERVER_HOST + "/rest/layergroups/" + layerGroupName + ".xml";
                WebRequest request = WebRequest.Create(gUrl);

                request.ContentType = "application/xml";
                request.Method = "PUT";
                request.Credentials = new NetworkCredential(GEOSERVER_USER, GEOSERVER_PASSWD);

                byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(newLayerGrpString);
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(buffer, 0, buffer.Length);

                WebResponse response = request.GetResponse();

                return true;
            catch (Exception e)
                return false;

This executes successfully. Tomcat logs also indicates successful execution of the PUT method. However, I can't see the created layer group using GeoServer web UI. Is there anything else I need to do after executing the above code? Trying to create another layer group with the same name fails with the error layer group already exists; a further proof that the layer group was created. But, why can't I see it using the GeoServer web UI. Any assistance will be highly appreciated.

share|improve this question
up vote 0 down vote accepted

After reviewing every step, I found that there was a slight problem with my XML. When inserting a new layer, <layer name="layer_name" /> is not acceptable to GeoServer, but it doesn't complain about it. It has to be <layer><name>layer_name</name></layer>. A C# method for constructing this XML is shown below. This now works fine:

private XmlDocument AddLayerNodeToLayerGroupXml(string layerName, string layerGroupString)

                XmlDocument layerGrpXml = new XmlDocument();
                layerGrpXml.Load(new StringReader(layerGroupString));

                XmlNode layerNode = layerGrpXml.SelectSingleNode("layerGroup/layers");
                XmlNode newLayerNode = layerGrpXml.CreateNode(XmlNodeType.Element, "layer", null);

                XmlNode newLayerNameNode = layerGrpXml.CreateNode(XmlNodeType.Element, "name", null);
                newLayerNameNode.InnerText = layerName;


                XmlNode styleNode = layerGrpXml.SelectSingleNode("layerGroup/styles");
                XmlNode newLayerStyleNode = layerGrpXml.CreateNode(XmlNodeType.Element, "style", null);


                return layerGrpXml;
            catch (Exception e)
                return null;

In the question above, this method is called at this line XmlDocument newLayerGrpXml = AddLayerNodeToLayerGroupXml(layerName, cLayerGroup);.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.