Creating a Container Using REST (C#)

[This document supports a preliminary release of a software product that may be changed substantially prior to final commercial release. This document is provided for informational purposes only.]

As described in Creating a Container Using REST, the following C# sample creates a container in the specified authority. The application performs the following steps:

  • Create an XML payload describing the container.
  • Initialize a WebRequest instance with the authority URI as the parameter. This is the authority in which the new container is created.
  • Set the Content-Type request header to application/x-ssds+xml indicating XML payload.
  • Send the request.
  • Process the response by printing the system assigned container version returned in the ETag response header.
Credentials are required to access the service. To make a working sample, update following code and provide your existing authority id and unique id for the new container. Each container beneath an authority must have unique id.

If you are using the Microsoft Visual Studio integrated development environment, create a console application and add the following code.

using System;
using System.Text;
using System.Net;
using System.IO;

namespace CreateContainerUsingREST
    class Program
        // Provide your own credentials
        private static string userName;
        private static string password;
        private const string authorityId = "<YourExistingAuthorityId>";
        private const string sampleContainerId = "<IdForTheNewContainer>"; 

        private const string ssdsContentType = "application/x-ssds+xml";
        private const string HttpPostMethod = "POST";

        static void Main(string[] args)
            Console.Write("Username: ");
            userName = Console.ReadLine();
            Console.Write("Password: ");
            password = ReadPassword();

            string AuthorityUri = String.Format("https://{0}",
            string containerUri = CreateContainer(AuthorityUri);

        private static string CreateContainer(string authorityUri)
            if (String.IsNullOrEmpty(authorityUri))
                throw new ArgumentOutOfRangeException("AuthorityUri");
            string containerUri = null;
                // We need to create XML Payload for the 
                // new container request.
                const string ContainerTemplate = 
                    @"<s:Container xmlns:s=''>
                // Data going over to the server
                string requestPayload = string.Format(ContainerTemplate, sampleContainerId);

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(authorityUri);
                // Security requirement - need to pass userid, password
                request.Credentials = new NetworkCredential(userName, password);

                // POST=create; PUT=update; DELETE=delete; GET=retrieve
                request.Method = HttpPostMethod;
                UTF8Encoding encoding = new UTF8Encoding();
                request.ContentLength = encoding.GetByteCount(requestPayload);
                request.ContentType = ssdsContentType;

                // Write the request data over the wire (1st container).
                using (Stream reqStm = request.GetRequestStream())
                    reqStm.Write(encoding.GetBytes(requestPayload), 0,

                // Get the response and read it in to a string.
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    if (response.StatusCode == HttpStatusCode.Created)
                        Console.WriteLine("Container created. System assigned container version: {0}", response.Headers["ETag"]);
                        // Since current imlementation sets response.Headers[HttpResponseHeader.Location] to
                        // null, we construct container uri manually
                        containerUri = string.Format("{0}/{1}", authorityUri, sampleContainerId);

                        Console.WriteLine("Failed to create container");

            catch (WebException ex)
                using (HttpWebResponse response = ex.Response as HttpWebResponse)
                    if (response != null)
                        string errorMsg = ReadResponse(response);
                        Console.WriteLine(string.Format("Error: {0}", errorMsg));
                        Console.WriteLine("Unexpected status code returned: {0} ", response.StatusCode);

            return containerUri;

        public static string ReadResponse(HttpWebResponse response)
            // Begin by validating our inbound parameters.
            if (response == null)
                throw new ArgumentNullException("response", "Value cannot be null");

            // Then, open up a reader to the response and read the contents to a string
            // and return that to the caller.
            string responseBody = "";
            using (Stream rspStm = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(rspStm))
                    responseBody = reader.ReadToEnd();

            return responseBody;

        private static string ReadPassword()
            StringBuilder retval = new StringBuilder();
            ConsoleKeyInfo keyInfo;
            while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter)

            return retval.ToString();


To verify results, specify the container URI in the browser:

When querying for non-blob entities your browser need to be aware of the application/x-ssds+xml SSDS content type. For Internet Explorer this can be done by adding a new key in the registry. For more information, see Guidelines and Limitations.

It returns container metadata as shown in the following sample XML:

<s:Container xmlns:s="" 

You can specify an empty query with the specific authority in the scope to find all the containers in it. For details about queries, see Querying SQL Data Services.