Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 am using C# and the WebRequest class to create a worksapce and then another WebRequest to create a datastore and upload a folder full of shapefiles to a geoserver. However, I would also like to change the style of the uploaded shapefiles to another style that has already been installed on the geoserver. Any ideas?


share|improve this question
up vote 0 down vote accepted

Ok, this is what I did....

Assumptions: I have a List<string> named layers that contains my layer names, I have a string called style that contains the style name, I have a class SessionConfig defined as follows and being populated with appropriate values (url, username, password etc):

static class SessionConfig
    public static string url = null;
    public static string username = null;
    public static string password = null;
    // public static string locationOfCurl = null;
    public static string workspaceName = null;
    public static string storeName = null;
    public static string locationOfShapeFiles = null;

Then the code to change the style of each layer in my workspace/datastore is as follows:

    string responseFromServer = null;

            for (int i = 0; i < layers.Count; i++)
    WebRequest request = WebRequest.Create(SessionConfig.url + "/rest/layers/" +
                                               SessionConfig.workspaceName + ":" + layers[i]);
                    request.ContentType = "text/xml";
                    request.Method = "PUT";
                    request.Credentials = new NetworkCredential(SessionConfig.username, SessionConfig.password);

                    byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("<layer><defaultStyle><name>"+style+"</name></defaultStyle><enabled>true</enabled></layer>");
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(buffer, 0, buffer.Length);
                    WebResponse response = null;

                    bool done = false;

                        while (!done)
                            response = request.GetResponse();
                            done = true;
                    catch (Exception e)

I'm not sure if this is 100% good code but it works for me.

share|improve this answer
If this is your solution please make this answer as accepted. – Chad Cooper May 19 '13 at 14:18
I'm not sure how to do this. – Lazaros Filippidis May 19 '13 at 18:19
By the vote count and up/down vote button, there is a greyed out check Mark. Check it to make it green. That marks it as answered. – Chad Cooper May 20 '13 at 1:39
Thank you for the tip. – Lazaros Filippidis May 20 '13 at 8:27

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.