Click here to Skip to main content
Click here to Skip to main content

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

, 25 Feb 2014
Rate this:
Please Sign up or sign in to vote.
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. Smile | :)

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)

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)

Share

About the Author

No Biography provided

Comments and Discussions

 
QuestionExactly what I need, unfortunately having trouble with the certificate [modified] PinmemberMember 31152427-Feb-14 5:48 
AnswerRe: Exactly what I need, unfortunately having trouble with the certificate PinmemberMantri Vishal27-Feb-14 6:57 
AnswerRe: Exactly what I need, unfortunately having trouble with the certificate PinmemberMember 31152427-Feb-14 7:58 
GeneralRe: Exactly what I need, unfortunately having trouble with the certificate PinmemberMantri Vishal27-Feb-14 16:24 
Thanks!!!
 
In my case, I created a Web Application / Console Application. When I run my code locally (i.e. Web Site or Console App) its working fine.. But when I Publish the code to Azure Web Site, I am getting the same issue with the Certificate.Even I upload the certificate along with the code as well and create the certificate using the MapPath in my C# code.. but still I am facing the same issue.. May be because Azure web site works on different VMs and not able to read my certificate file..
 
Looking for some solution.. if you have please share your code...
 
Thanks again!!!
GeneralRe: Exactly what I need, unfortunately having trouble with the certificate PinmemberMantri Vishal27-Feb-14 22:09 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 25 Feb 2014
Article Copyright 2014 by Mantri Vishal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid