Sunday, August 19, 2012

Windows 8 “Metro” Apps Update

When the Windows 8 developer preview came out I wrote some articles talking about how to develop the new “Metro” style apps. Now that Windows 8 has gone RTM (if you have an MSDN subscription you can download Windows 8 and Visual Studio 2012 right now) I thought I would update the information in those articles.

The first article was called Windows 8 Metro Apps.

The first big change between then and now actually came fairly recently. Due to a legal dispute Microsoft will no longer use the term “Metro” for apps developed for the new user interface in Windows 8. As to what they will now be called, there still seems to be some confusion over this. I have seen the terms “Windows 8 style” and “Modern UI” apps used but they don’t seem to have settled on a final name. To make things more confusing Visual Studio uses the term “Windows Store app”, and as of this writing you will still see the term Metro used in some of the documentation. This naming confusion should eventually settle out. Personally I am going to use the term WinRT app for now since these new style apps run in the new Windows Runtime environment.

Starting a New App

When you create a new project in Visual Studio 2012 the WinRT app templates can be found under each language in the Windows Store group. Also in the new project windows be sure to check out the item called Online. This will give you access to a large number of template and sample applications in various languages.

Developer License

When you try to create your first WinRT app you will be asked to apply for a Developer License. This is a free license and you will need it whether you plan to publish you app to the app store or not.  This license will be tied to your Microsoft account, so you will need one of those if you don’t already have one.

Other then this only other difference you will see when you run the app is the default icon has changed since the developer preview. It now looks like this:

image

Saturday, June 30, 2012

Salesforce REST API Read File

In my last post I showed how to upload a file to Salesforce, now lets look at how to read it back. This requires two steps, one to read the document object and a second step to read the actual file data.
Reading the document object is just like reading any other type of object in Salesforce. First we need a class to hold the document object.

public class sfdcDocument
{
    public string Description { get; set; }
    public string Keywords { get; set; }
    public string FolderId { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public string Body { get; set; }
}

Here is the code to retrieve the document object and de-serialize it.

var uri = instanceURL + "/services/data/v24.0/sobjects/Document/015E0000000qhId";

var req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
req.Headers.Add("Authorization: OAuth " + accessToken);
req.ContentType = "application/json";
req.Method = "GET";
  
var resp = req.GetResponse();
var sr = new System.IO.StreamReader(resp.GetResponseStream());
var result = sr.ReadToEnd();

// Convert the JSON response into a token object
JavaScriptSerializer ser = new JavaScriptSerializer();
sfdcDocument doc;
 
doc = ser.Deserialize<sfdcDocument>(result);


First we create the REST URI needed to retrieve the document. We are using the simplest method of reading the document here, by just specifying the id of the document we want to read. There are ways to query by filename but I will cover that at another time. Now we make an HTTP get request, setting the accesstoken to the value we got during the authentication process, then read the response and finally de-serialize it.

The document object contains a field called Body which contains the URI used to retrieve the actual binary file data. Here is the piece of code needed to do that.

System.Net.WebClient client = new System.Net.WebClient();
client.Headers.Add("Authorization: OAuth " + accessToken);
client.DownloadFile(instanceURL + doc.Body, @"c:\temp2\" + doc.Name );


Here we use a WebClient to simplify reading the file. As always we add the access token, then execute DownloadFile using the instanceURL you got along with the access token, and the Body property of the document to specify the whole URI. We can also use the Name property of the document to get the original filename for the document and save it using the same document name.