Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / Azure

Creating Table in Azure Mobile Services using Azure Service Management Rest API

5.00/5 (3 votes)
25 Feb 2014CPOL1 min read 17.9K  
How to creating table in Azure Mobile Services using Azure Service Management Rest API

Introduction

As you all know, Windows Azure Mobile Services comes with the rich set of SDKs for interacting with the backend. The SDKs are available in various platforms like .NET, JavaScript, etc. But for creating a table and listing of tables in the Mobile Services account, there is no feature available in the Mobile Services Resful API. This means one can perform the CRUD operation once the table is created in the azure portal.

In the code below, you can see how the table can be created using Service Management Rest API.

Background

I was trying to build a POC around the Mobile Services backend capabilities and wanted to create a table using the web interface (without actually going to the Azure account / Portal). But actually, I could not find any solution with the available Mobile Services SDK (.NET / JavaScript). Then one of my friends told me to explore the Service Management API and finally it worked. :)

Using the Code

Below is the code this will help you to create a Table in the Mobile Services.

There are 4 access levels on any of the tables to perform CRUD operation. I have used "Public" while defining the Payload:

1. User Only Authenticated Users
2. Public Everyone
3. Application Anybody with the application key
4. Admin Only scripts and Admins

Please make sure you should have both the Certificate files (i.e. .cer and .pfx) in your App_Data folder if you are making the Web Application and later publishing as the Windows Azure Web Site)

C#
string Url = "https://management.core.windows.net/
{YOUR SUBSCRIPTION ID}/services/mobileservices/mobileservices/{YOUR MOBILE SERVICENAME}/tables";

            HttpWebRequest request = WebRequest.Create(Url) as HttpWebRequest;

            request.Accept = "application/xml";
            request.ContentType = "application/xml";
            request.Method = "POST";
            request.Headers.Add("x-ms-version", "2012-03-01");

            string payload = @"<Table 
            xmlns=""http://schemas.microsoft.com/windowsazure/mobileservices"" 
            xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""> 
            <Delete>{0}</Delete> 
            <Insert>{1}</Insert> 
            <Read>{2}</Read> 
            <Update>{3}</Update> 
            <Name>{4}</Name> 
            </Table>";
            string tableContent = string.Format(payload, "Public", 
            "Public", "Public", "Public", {TABLE NAME});

            byte[] bytes = UTF8Encoding.UTF8.GetBytes(tableContent);
            try
            {
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(bytes, 0, bytes.Length);
                }
                
                var certPath = HttpContext.Current.Server.MapPath("../App_Data/{CERTIFICATE}.pfx");
                
                var certificate = new X509Certificate2(certPath, {PASSWORD});  

                request.ClientCertificates.Add(certificate);
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream);
                string responseString = reader.ReadToEnd();
                Console.WriteLine(responseString);
                Console.ReadLine();
            }
            catch (WebException ex)
            {
                HttpWebResponse resp = ex.Response as HttpWebResponse;
                Stream stream = ex.Response.GetResponseStream();
                StreamReader reader = new StreamReader(stream);
                string error = reader.ReadToEnd();
                Console.WriteLine(error);
                Console.ReadLine();
            } 

Conclusion

If anybody is trying to create a table for Mobile Service using C#, this will really be a great help.

Thanks for reading!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)