Working on some simple apps for SharePoint 365, I was struggling to find a good framework to display app settings. App model for SP365 is slightly different to what we had in SP 2010, and together with new API there bunch of new scenarios which you can use to make your app awesome. The good thing, which I personally like about new app model, is that now app has it’s own space. Like a subsite with ability to create lists and libraries especially for your app. Why will you need it? Emm.. Let’s say you have a visual web part, which displays World Time. So, inside you App Web Host you can have a list with cities and time zones which could be visible only yo your app, and as soon as your app deleted – all app lists will be deleted as well. So, AppWebHost is a good place to keep your app logic and settings.

Now, for the visual web part it is important to keep it small and clean, at the same time for the settings part of your app it could be good to have something easy to build. Lightswitch HTML client could be a good option together with different good features which Lightswitch support for SharePoint – like people pickers and rich integration with different data sources. The good article about LightSwitch SP integration could be found here: link

Ok, now time to dig a bit deeper. Read first the article here: link In this article Lightswitch team shows how SharePoint lists could be used in the app. However, we know that SP Lists which are going to be deployed with app, will be created when the app will be installed to SP server. It means that we are not able to reference them as a data source in our lightswitch app, because they are just simply don’t exists, until the app is deployed. So, from the article above Lightswitch team uses SP client object model to insert records when there are some CRUD operation with another data source. Let’s say a new customer has been created – insert record in SP list using client object model. But what if I need to display records form the AppWebHost list? What if I want to perform CRUD operations directly on the list itself?

Here is a solution.

1) Creating Lightswitch HTML app.

First we need to create a Lightswitch HTML application and enable SharePoint.


After you need to specify SharePoint site which is going to be used by Lighswitch for debugging. After some time Lightswitch should come up with credential page for you SP environment. As soon as everything verified Visual Studio should come up with something similar to this:


At this stage you can hit F5 and app should be installed and lunched from your SP.

2) Creating simple WCF RIA service.

2.1. Now we need to create a WCF RIA service, which will be used as our data source for our app.

2.2. Right click on the colution and add new Project – Class Library. Also delete created class and add new class called SpList.

2.3. Add references to:

2.4. Here is a source code of SpList class.

So in this class we have 4 methods- Query, Insert, Delete and Update.

For now we only implement the first one. Let’s compile it and add as a DataSource to Lightswitch.



2.4 Now, let’s create a new Browse screen and select as a Screen Data the table which just added.


Let’s run the app:


The last thing we need to do is to add Delete, Add buttons and Delete method. Also implement item tap event for View/Edit item.


Code for Delete method:

Run the app:


Hit Add Button:


For now we finish here. Let’s implement SharePoint Authentication:

3) Implementing SP OAuth.

When we have a provider hosted app, the only way for us to talk to Sp is to use OAuth. When you create a new app for SharePoint, VS will generate for you two files:

TokenHelper.cs and SharepointContext.cs


We will need to use and modify TokenHelper. TokenHelper is a class responsible for getting clientContext by using ClientID and Client Secret.

You can Download TokenHelper class here: TokenHelper


Add class to the WCF RIA project. VS should display something around 50 errors. We need to add some references.


After you get TokenHelper successfully built, it is time to implement all methods for RIA service. But before that, we need to let know the WCF RIA service, what is AppWebHost url, If we open AppManifest file for Sp project we will see general settings for the App and which page will be called after the app is lunched in SharePoint:


It means that SP will pass all StandardTokens to SharePointLaunch.aspx. Our problem here is that WCFRia service doesn’t know anything about these parameters. Even if we are running WCF RIA service from the AppWebHost_Lists.Server, we are not able to get the same HttpContext. However WCF RIA service still have an access to Http Session. It give us an option to pass Standard Tokens through HttpContext Session:

Let’s start. Open SharePointLaunch.aspx and override Page_Load method:

Now we need to override Initialize method for DomainSevice. Return to our SpList class and add this:

Having AppWebHost Url we are able now to access Lists from AppWebHost.

First let’s create a new List:


Hitting Finish button should make your SP project looking similar to this:


Settings List will be available for us as soon as app is installed on the server.

So, it is time to modify our Query, Insert, Update and Delete methods.

Complete code for SpList class:

After you run the application you should be able to perform all CRUD operations.

Code for the solution could be found here: source

Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">