Developer Reference

Developing for Windows Phone 7

Let’s assume that you’re already read getting starting guide of how to use CloudyRec Web interface.
In this example, I have created the app called “Bookmark Manager” using web interface and already had the following structures.
I also exported the code for WP7 using the code generator module offered by CloudyRec.

Downloaded source
Fig 1.1: Define the resources for new app

Downloaded source
Fig 1.2: Generate the require source code for WP7

1. Creating a new Windows Phone 7 project in Visual Studio

Extract the downloaded zip file to your workspace and change folder name also as we can see in the following figure, it has all the necessary files to start an Windows Phone 7 project development.

Directory contents
Fig 1.3: WP7 source directory contents

Directory contents
Fig 1.4: WP7 source directory contents

So in Visual Studio, we can create a new Visual Studio project from existing source by selecting the source code folder you just downloaded from CloudyRec and drag it into the existing project that you just created.
Directory contents
Fig 1.5: WP7 Creating a new project

Directory contents
Fig 1.6: Add the CloudyRec source code to the existing project.

Now it’s time to add the libraries used by CloudyRec.You can do this by adding references to the libraries that’s already packaged in the CloudyRec\Library folder that you’ve added into the project
Directory contents
Fig 1.7: Adding references to the project

Your project folder will look something like this.

Visual Studio project directory
Fig 1.8: Visual Studio project directory

It’s also important to add the namespaces and datatypes in source files on every page.
Visual Studio project directory

using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using Hashtable = System.Collections.Generic.Dictionary<object, object>;
using ArrayList = System.Collections.Generic.List<object>;

Fig 1.9: Adding additional namespaces

2. Using resource methods

CloudyRec generates each resource we defined as an individual class with OOP designs in mind. That means if we define 2 resources as user and blog_post, we’ll get 2 classes with their respective names. In the following code examples we’ll use the ‘user_account’ resource we created previously from the Web Interface.\n\nWindows Phone 7 enforces to use the Async data connection so we also provide the Callback function that the develper can pass during the function and give them freedom to manage the callback datas.

2.1. Creating data

Creating a new record can be done in multiple ways. The simplest way is to pass all the required attribute values to the constructor when creating a new resource object. Then call the save method to upload the data to CloudyRec.

UserAccount user = new UserAccount("user id", "secret pwd");
user.save(CallbackSave);

private int CallbackUserSave(RestResponse response)
{
	//UserAccount Save Callback
	if ((int)response.StatusCode == 201)
	{
		string id = response.Content;
		MessageBox.Show("Save Successful.\n ID : " + id);
	}
	else
	{
		MessageBox.Show("Connection Error : " + response.Content);
	}

	return 0;
}

2.2. Authenticating an existing user

If a resource is being checked as “is authenticable” during resource creation, we can use the generated authenticate method to login with a user id and password.

UserAccount user = new UserAccount();
user.Authenticate(txtBox_Username.Text, txtBox_Password.Password, CallbackAuthenticate);

2.3. Retrieving data

To retrieve the records from CloudyRec servers, you can use the List method. This method accepts a parameter to specify selection filters. If you pass null, it’ll return all the records from that resource.
Selecting all users:

UserAccount user = new UserAccount();
user.UserAccountList(null, null, true, 10, 1, CallbackUserAccountList); //No Query and no Sorting if passed null for the first two parameters

Iterating through the list:

//Parse and display user list in the list view
private int CallbackUserAccountList(RestResponse response)
{
	ArrayList objList = new ArrayList();
	JArray objArray = JArray.Parse(response.Content);
	foreach (JObject data in objArray)
	{
		UserAccount obj = new UserAccount();
		obj.SetData(data);
		objList.Add(obj);
		//Assign the username and password to view
		string uname = (string)data["_uname"];
		string pwd = (string)data["_passwd"];
		listView_UserAccounts.Items.Add(uname + "\n" + pwd);
	}
	return 0;
}
}

Selecting a particular user:

UserAccount user = new UserAccount();
user.UserAccountList("_uname=user_id", null, true, 10, 1, CallbackUserAccountList);

2.4. Updating data

In order to edit and update a record, we first need to get the record ID that we want to edit. Then update its properties through getter/setter methods and call the save method again.

Updating a particular user:

UserAccount user = new UserAccount();
user.id = <yourAppID From Authentication or somewhere else>;
user.passwd = <Your New Password or something>;
user.Save(CallbackSave);

2.5. Deleting data

Deleting a record is similar to updating a record. We first get the record ID we want to delete and the call the delete method. Let’s delete all the records from our user resource.

Deleting a particular user:

UserAccount user = new UserAccount();
user.id = <yourAppID From Authentication or somewhere else/;
user.Delete();

2.5. Callback function example:

Here is the example callback function that a developer might call on the user authentication step.

private int CallbackAuthenticate(RestResponse response)
{
	if ((int)response.StatusCode == 200)
	{
		JObject data = JObject.Parse(response.Content);
		string strID = (string)data["id"];       //Hash ID
		string strUName = (string)data["_uname"];    //username
		MessageBox.Show("Login Successful\nID : " + strID + "\nUsername : " + strUName);
	}
	else
	{
		MessageBox.Show("Connection Error : " + response.Content);
	}
	return 0;
}