Banner Advert
Luc Stakenborg
Posted on March 18, 2012 by
Leave a comment

This is a guest post from AllThatJS. This post comes from Luc Stakenborg, and you can find the original post here

In this post we will explore how to build a SharePoint mobile web app using Sencha Touch, a great mobile JavaScript framework. SharePoint 2010 is a very comprehensive platform, but mobile support is fairly limited. Although it is technically possible to customize the mobile user interface on the server side, this is not allowed in cloud based SharePoint (e.g. in Office365).

An alternative  approach is to build a web app where the UI is generated in the browser and connects to one of the SharePoint APIs to access data stored in SharePoint. SharePoint 2010 offers three data APIs which can be used in web apps:

  • SP Web Services (SOAP)
  • REST (ListData.svc)
  • CSOM (Client Side Object Model)

Although each of the APIs offer a distinct set of capabilities, the REST interface is simple and lightweight and I prefer this for mobile use. The SharePoint REST interface offers full CRUD access to items in SharePoint lists and libraries, which might be all that you need for your mobile web app.

Getting started
Because of the Same Origin Policy, your html file must be served from the same domain as the SharePoint site you want to access. You can place your html file containing your app on the server file system or in a SharePoint document library (e.g. site assets).

If you are using SharePoint Online (Office365), you will notice that when you select a .html file in a doc library, it is presented as a download, not opened in the browser. This is due to SharePoint settings which you are not allowed to change in SharePoint Online. As a workaround, simply use an .aspx file extension, instead of .html. This way, you can start your single page SharePoint application from a file in an asset library.

So, to get going, you need to create a app.aspx file to include all the css and js for your Sencha Touch app.

<html>
    <head>
    <title>SharePoint web app exampletitle>
       <link rel="stylesheet" href="sencha-touch.css" type="text/css" />
       <link rel="stylesheet" href="app.css" type="text/css" />
       <script src="sencha-touch.js">script>
       <script src="app.js">script>
    head>
    <body>
    body>
html>

The app.js file contains the basic Sencha Touch start-up and shows an alert:

image
Now you can open up the following url on your iPhone or Android device:

http:///[spserver]/siteassets/app.aspx

After logon, you will see the following result:

IMG_0724
Not quite what we expected… SharePoint has detected that we access the page with a mobile device and responds with the default mobile UI. You can suppress the default mobile UI by appending ?mobile=0 to the url.

So, let’s try: http:///[spserver]/siteassets/app.aspx?mobile=0

IMG_0725
Yes! We now have a Sencha Touch web app running of a SharePoint server.


OData proxy

The next step is to connect Sencha Touch models and stores to SharePoint items and lists through the REST interface using the OData protocol. For this you will need an OData proxy. I developed an OData proxy as a Ext user extension. It is designed to access SharePoint data using the SharePoint ListData.svc REST service which is based on OData. You may use it for other OData sources.

The OData SharePoint proxy is on GitHub: https://github.com/lstak/SharePoint-proxy-for-Sencha-Touch

Ext.ux.ODataProxy features:

  • create, read, update and delete SharePoint items as Sencha Touch models
  • fetch multiple items from a SharePoint list in a Sencha Touch store
  • JSON payloads
  • partial updates: only changed attributes are sent to the server during an update
  • fixes issues in Sencha Touch data Model implementation (e.g. missing destroy() method)

Let’s look at some examples how you can use the SharePoint proxy. In these examples we will assume you have a subsite ‘/teamsite’ in which you have created a Contacts list based on the standard Contacts list template.

First we need to define the Model.

01 var Contact = Ext.regModel('Contact', {
02   fields: [
03       // please note CamelCase convention for SharePoint column names
04       { name: 'Id', type: 'int' },
05       'LastName',
06       'FirstName'
07     ],
08
09   idProperty: 'Id',
10
11   proxy: {
12     // use the special odata proxy defined in odataproxy.js
13     type: 'odata',
14
15     // the proxy will connect to the List
16     // named 'Contacts' in the /teamsite subsite
17     url: '/teamsite/_vti_bin/ListData.svc/Contacts'
18   }
19 });

We can now use the following CRUD operations on the Contact data model:
01 // Create an instance
02 var contact = new Contact({ LastName: 'Johnson', FirstName: 'Al' })
03 contact.save();
04 ...
05
06 // Read an instance from the server by id
07 var id = 200;
08 Contact.load(id);
09 ...
10
11 // Update an instance, loaded from the server
12 Contact.load(id, {
13     success: function (contact) {
14         contact.set("LastName""Maxwell");
15         contact.save();
16     }
17 });
18 ...
19
20 // Delete an instance
21 Contact.load(id, {
22     success: function (contact) {
23         contact.destroy()
24     }
25 });
26 ...

Using the Contact model you can now easily define a Store to fetch multiple items:
1 var store = new Ext.data.Store({
2     model: 'Contact'
3 });
4
5 store.load()


Build your application

Using the odata proxy to configure your Models and Stores that connect to the SharePoint server, you can develop your app further just like any other Sencha Touch app. Check out the tutorials on the Sencha site for more info.

No comments yet, why not be the first?

Add a comment