Windows Phone Enterprise Apps for Sharepoint

Posted by: Mayur Tendulkar , on 2/4/2014, in Category Windows Phone
Views: 38074
Abstract: A walkthrough of how we can build an enterprise Windows Phone application which can easily integrate with Office 365 SharePoint site and perform some common tasks

Apart from daily calls & text messaging, people use their cell-phone to check their emails, connect on social networks, listen to music, take photographs or play some games. There are many use cases for mobile devices. However for an enterprise, mobile devices make sense when it helps to improve productivity. Microsoft being an enterprise game player and having technologies like Azure, Office 365, SharePoint, etc… makes using Windows Phone in enterprise much more easier. Concepts like BYOD (Bring Your Own Device), MDM (Mobile Device Manager) helps us to be more productive in the organization using our own mobile phones. In this article we’ll see how we can build a Windows Phone application which can easily integrate with Office 365 SharePoint site and perform some common tasks.

This article is published from the DotNetCurry .NET Magazine – A Free High Quality Digital Magazine for .NET professionals published once every two months. Subscribe to this eMagazine for Free and get access to hundreds of free .NET tutorials from experts

 

Introduction to Office 365

Office 365 is a cloud based service from Microsoft, which is a suite of different applications like SharePoint, Exchange, Lync, etc. SharePoint is a collaboration platform, which again provides lot of functionalities like document management system, team-sites, etc. This is a highly extensible platform and using it on mobile device, can give a boost to productivity.

For this article, we’re going to use a trial version of Office 365. If you do not have one, register for one here: bit.ly/dncm9-off365reg

Once you’ve created your Office 365 account, wait for a while so that it sets up collaboration tools like SharePoint (team-site), Newsfeed, SkyDrive Pro, etc…

Creating Development Environment

To develop apps for SharePoint (Office 365), we’ll create a demo site and create a list under it to store some data. Follow these steps:

1.Locate a ‘Settings’ icon on right-hand top corner and click on ‘Add an app’ option.

windows-phone-add-app

2.On next page, click on Custom List

custom-list

3.In the ‘Adding Custom List’ dialog box, give it a name and click on ‘Create’

demolist-contents

Building Windows Phone Application

As our SharePoint (Office 365) development environment is ready, we can now build Windows Phone application to communicate with it. SharePoint exposes its API as Client Side Object Model (CSOM) as well as REST services. So, while building mobile apps for SharePoint, developers can either make REST calls and manage the app cycle manually i.e. authentication, session management, sending/retrieving data etc… OR use CSOM libraries to use simple Object Model provided by SharePoint. Microsoft has released a Microsoft SharePoint SDK for Windows Phone 8 to make life much easier. This SDK installs the CSOM libraries for Phone along with authentication libraries and project templates. This CSOM installed by the SDK is similar to CSOM available on desktop. We’re going to use the SDK in this article. You can download and install the SDK from here: http://bit.ly/19yXi5w

Creating Windows Phone App for SharePoint

When you install the SDK, it automatically adds project templates to existing Visual Studio installation. Let’s go through these templates.

phone-sharepoint-templates

In the screenshot, you can see two project templates are installed during SDK installation. One is Windows Phone Empty SharePoint Application. You can use this template to build apps from scratch and have custom functionality around SharePoint on Phone. The second one is Windows Phone SharePoint List Application. This project template is useful if you want to manipulate CRUD operations on a specific list. First, we’ll build a blank app and then explore the List application.

Creating Windows Phone Empty SharePoint Application

Click File > New Project. Select ‘Windows Phone Empty SharePoint Application’ project template under C# and give it a name.

When you create a blank app, by default following assemblies are referred.

Microsoft.SharePoint.Client.Phone
Microsoft.SharePoint.Client.Phone.Auth.UI
Microsoft.SharePoint.Client.Phone.Runtime

You can check these under Project > Right Click > Add Reference window

sharepoint-connector

In the design screen (MainPage.xaml), add following code inside ContentPanel to create a list to show records.




Now, in code-behind file (MainPage.xaml.cs), add an event handler for ‘Loaded’ event inside constructor. The code should look like the following:

// Constructor
public MainPage()
{
InitializeComponent();
       Loaded += MainPageLoaded;
}
void MainPageLoaded(object sender, RoutedEventArgs e)
{
}

To hold lists from SharePoint site and its items, we’ll declare (just above the constructor) two private variables as:

private List _list;
private ListItemCollection _collection;

Creating Context for Further Requests

Just like HttpContext, SharePoint provides ClientContext, which maintains authentication data, sends requests on server and retrieves data to client applications. We’ll create a single instance public static ClientContext which we can access from anywhere inside the app to execute all our further request. We’ll declare it in App.xaml.cs using following code.

//Private instance of ClientContext
private static ClientContext _mContext;
//Public property returning client context
public static ClientContext Context
{
get
       {
       if (_mContext != null)
           return _mContext;
//Create object of ClientContext by passing SharePoint Site URL
       _mContext = new ClientContext("https://tendulkars.sharepoint.com/demosite");
       //Create authenticator which will hold authentication data for context
       var at = new Authenticator {CookieCachingEnabled = true};
       //Use at.AuthenticationMode property to set auth mode. Nothing for default
       //Set authenticator to context
       _mContext.Credentials = at;
       return _mContext;
       }
}

This ClientContext can be accessed from anywhere within the application using App class.

Retrieving Data from SharePoint List

When developing SharePoint applications, you need to understand the model and how it works. For simplicity, you can think of it as a big web application, each web app having SiteCollections and each site collection (may have sub-sites) having Lists. These lists will hold the actual data in columns and rows format. And as a rule, you must first populate (or load) web application, then sites, etc… down the line. Following code will perform the same actions. I’ve added comments to each line for details

void MainPageLoaded(object sender, RoutedEventArgs e)
{
//Get the top level Web site
       var web = App.Context.Web;
       //Fetch the desired list by title inside the web selected above
       _list = web.Lists.GetByTitle("DemoList");
       //Create a query to load all items/fields in that list
       var query = CamlQuery.CreateAllItemsQuery();
       //Fetch the items from that list using above query
       _collection = _list.GetItems(query);
       //Load the web app
       App.Context.Load(web);
       //Load the collection
       App.Context.Load(_collection);
       //Execute the query NOW providing Success & Failure callbacks
       App.Context.ExecuteQueryAsync(SucceededCallback, FailedCallback);
}

Just like LINQ queries have deferred execution, in SharePoint ClientContext, code executes when ExecuteQuery method is called populating items top to bottom. In this case, when ExecuteQuery is executed, first web, then list and at last list items are populated.

Now, depending on the execution call-back functions will be called. These are written as follows:

private void FailedCallback(object sender, ClientRequestFailedEventArgs args)
{
MessageBox.Show("FAIL");
}

private void SucceededCallback(object sender, ClientRequestSucceededEventArgs args)
{
//Create temp list to hold items
       var list = new List();
       //Iterate through all the items
       foreach (var item in _collection)
       {
           //Get title and add to temp list
              list.Add(item["Title"].ToString());
}
       //On UI thread bind temp list to ListBox
       this.Dispatcher.BeginInvoke(() =>
       {
           DemoList.ItemsSource = list;
});
}

At this time, your application is ready to run. Just hit F5 to test it inside emulator. The first screen you should see is Office 365 authentication window. Provide your login credentials here:

o365-auth

Once you provide these details, click on sign-in and you should be taken to the main page, which will be showing all the items from SharePoint list.

list-data

Creating Windows Phone SharePoint List Application

We just created a Windows Phone SharePoint Application from scratch, which retrieves data from SharePoint list and shows inside the app. But in a real-life scenario, you may want to perform all CRUD operations on it. You may want to show more data, with more actions. Certainly, you can extend this app to do so. But for the same reason, Microsoft provides Windows Phone SharePoint List application. This project templates does all the job without writing a single line of code. The auto-generated code follows Model-View-ViewModel (MVVM) pattern and is production ready. Let’s build the same app using this template. Just create a new project with Windows Phone SharePoint List Application project template selected.

When you create SharePoint List Application, the next dialog will appear. On this screen, type-in your SharePoint site URL containing the list which you want to work on. After that click on ‘Find Lists’.

sharepoint app wizard

This brings up the Office 365 Authentication box. Provide your authentication details here and sign-in. In the next screen, you should see all the lists available to you.

sharepoint-list

Choose the list and click on ‘Next’.

In the next dialog box, select the items that you want to be shown and processed on mobile device. This is kind of a filtering criterion i.e. top 50, all, last 10, etc… Select All Items here as we can change this value later on. Now click on ‘Next’

choose-views

Next you should be able to select actions that you want to perform on the items you selected on the previous screen, like add, edit, delete etc…

choose-phone-actions

Next you can choose the ‘fields’ or ‘columns’ from list that you want to display on the screen. Select those fields and click next.

sharepoint-choose-fields

In the last screen, you can choose the display order of the fields. In this case, we’ve just one field, but in case of multiple fields, you can easily re-arrange the order by pressing Up-Down arrow keys.

wizard-5

When you click on the ‘Finish’ button, a Windows Phone project is created. This project is built using Model-View-ViewModel (MVVM) pattern. You can see the structure of project in Solution Explorer.

project-view

Now when you run this project, you should see the output as shown below

list-data-all

Conclusion

In this article we saw how we can build Windows Phone applications which can easily integrate with enterprise solutions like SharePoint Online (Office 365). We’ve used Microsoft SharePoint SDK for Windows Phone 8 to make many tasks simpler. Furthermore, you can use services like Windows Intune or Company Store to manage and publish apps to selected devices within enterprise. We’ll discuss more about these features in the upcoming magazine edition.

Download the entire source code from our GitHub Repository at bit.ly/dncm9-wp8sp

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Mayur Tendulkar works as a Local Type Inference (i.e. Consultant) at Zevenseas. He has worked on various Microsoft technologies & is part of Pune and Mumbai User Groups. Since 2008, he has been a speaker at various Microsoft events. You can reach him at mayur[dot]tendulkar[attherate]hotmail.com


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook