Click here to Skip to main content
15,881,588 members
Articles / Hosted Services / Azure
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
25 Feb 2014CPOL1 min read 17.2K   2   5
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)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionExactly what I need, unfortunately having trouble with the certificate Pin
Member 31152427-Feb-14 5:48
Member 31152427-Feb-14 5:48 
AnswerRe: Exactly what I need, unfortunately having trouble with the certificate Pin
Mantri Vishal27-Feb-14 6:57
Mantri Vishal27-Feb-14 6:57 
AnswerRe: Exactly what I need, unfortunately having trouble with the certificate Pin
Member 31152427-Feb-14 7:58
Member 31152427-Feb-14 7:58 
GeneralRe: Exactly what I need, unfortunately having trouble with the certificate Pin
Mantri Vishal27-Feb-14 16:24
Mantri Vishal27-Feb-14 16:24 
GeneralRe: Exactly what I need, unfortunately having trouble with the certificate Pin
Mantri Vishal27-Feb-14 22:09
Mantri Vishal27-Feb-14 22:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.