CHAPTER 14

image

Using Other App Environments

Throughout this book, we have explored the various ways an app for SharePoint can leverage the resources of the SharePoint farm. The components that make up our apps have been hosted locally or in remote web sites outside of SharePoint to enable us to use server-side ASP.NET logic. In all cases, the custom logic for our apps has executed outside of the SharePoint server farmin a web server, a web service, or a web browser. In spite of this separation, we have always had a connection back to the SharePoint farm, in the form of context or access tokens. We have had these because our apps were always launched from inside SharePoint.

In this chapter, we will step outside of SharePoint and look at how to access SharePoint resources without using apps for SharePoint. In doing so, we will go over the following points:

  • We will describe how an application running outside of SharePoint can perform authentication and access resources within the farm.
  • We will integrate the Microsoft Office client applications, such as Word and Excel, with our SharePoint app solutions.
  • We will create applications that access SharePoint while runningasa Windows .NET application or in the Windows Runtime (WinRT).
  • We will extend the reach of our SharePoint data to include native Windows Phone apps.

Unlike in previous chapters, we will not be focusing on building SharePoint apps here. The applications in this chapter will access SharePoint from an untrusted connection, requiring the application to log on to SharePoint in order to ensure privacy and data integrity.

Authenticating with SharePoint

When a user launches an app for SharePoint, the server records the current user, the app, and other information making up the context of the app. SharePoint uses this information to create a context token, which is provided to the app as part of the page request that launches the app. The context token is then used to make requests back into the SharePoint server in a secure fashion.

When an app outside of SharePoint wishes to make a request into SharePoint, there is no pre-existing context with which to work. This means that the application is responsible for authenticating with SharePoint before any other requests can be processed. This process is sometimes referred to as active authentication. It is active in the sense that the application must actively perform the authentication process in its code. The authentication performed by SharePoint when launching app could be called passive authentication. The only difference is whether the authentication process is initiated by SharePoint or the application.

In order to access SharePoint through one of the client-side object model libraries, the REST interface or one of SharePoint’s native web services, the results of either active or passive authentication must be provided in the client request. Later in this chapter, we will describe some of the techniques for performing active authentication and passing the results to SharePoint. In this section, we will disassemble the authentication process to understand what is happening under the covers.

Figure 14-1 illustrates the sequence of messages that must be passed between the application, the Security Token Service (STS), and the SharePoint farm. These messages occur in three request/response pairs. The first pair establishes the user’s identity with the STS. The second “logs in” to SharePoint by supplying this identity to SharePoint. The third pair represents one or more client requests from the application to SharePoint.

9781430258841_Fig14-01.jpg

Figure 14-1. Active authentication message sequence

Step 1 in the figure involves formatting a request for the Security Token Service. When using SharePoint Online, the STS role is assumed by the Windows Azure Access Control Services (ACS). When running SharePoint in a local server farm, SharePoint’s own STS is used. The request is formatted as a Security Assertion Markup Language (SAML) request. The SAML request is an XML document that contains the URL of the target SharePoint site, user name, and password, as shown in Listing 14-1.

Listing 14-1.  Sample SAML Login Request

<s:Envelope
xmlns:s=" http://www.w3.org/2003/05/soap-envelope "
  xmlns:a=" http://www.w3.org/2005/08/addressing"
xmlns:u=" http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<s:Header>
<a:Action
 s:mustUnderstand="1"> http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue </a:Action>
<a:ReplyTo>
<a:Address> http://www.w3.org/2005/08/addressing/anonymous</a:Address >
</a:ReplyTo>
<a:To s:mustUnderstand="1"> https://login.microsoftonline.com/extSTS.srf</a:To >
<o:Security s:mustUnderstand="1"
        xmlns:o=" http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd ">
<o:UsernameToken>
<o:Username> MyUserName </o:Username>
<o:Password> Pass@word1 </o:Password>
</o:UsernameToken>
</o:Security>
</s:Header>
<s:Body>
<t:RequestSecurityToken xmlns:t=" http://schemas.xmlsoap.org/ws/2005/02/trust ">
<wsp:AppliesTo xmlns:wsp=" http://schemas.xmlsoap.org/ws/2004/09/policy ">
<a:EndpointReference>
<a:Address> https://swrightjet.sharepoint.com</a:Address >
</a:EndpointReference>
</wsp:AppliesTo>
<t:KeyType> http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType >
<t:RequestType> http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType >
<t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType>
</t:RequestSecurityToken>
</s:Body>
</s:Envelope>
 

When the STS receives the SAML request, it verifies that the format of the request is correct, that the request is from a known source, and that the request is for a known target site. If the request is valid, the STS attempts to verify the login information. This may involve accessing the Microsoft Online Directory Service (MS-ODS) or passing the request to a federated identity service for validation. In step 2, the result of this validation is sent to the requesting application. If the request was successful, the STS returns a security token that has been generated for this session.

The application passes the supplied SAML security token to the SharePoint server as proof of the user’s credentials in step 3. Note that the user’s password is not included in this token. Only the STS ever sees the user’s password. SharePoint validates that the security token is from a trusted STS and for a valid resource in SharePoint.

In step 4, SharePoint formats two cookies that are returned to the calling application. These are called FedAuth and rtFa. These cookies are represented by the context token in an app for SharePoint. In the remote application, these cookies are saved to allow for future client requests into the SharePoint site.

The application will format a request for SharePoint using one of the client APIs for SharePoint. The request can be a CSOM batch, or a WebRequest containing a REST interface call or web service call. In order to be processed by SharePoint, the authentication cookies provided by SharePoint must be included in the request. This is step 5.

Only requests that contain the correct authentication cookies will be acted upon by SharePoint. The CSOM libraries’ client content object adds these cookies automatically when using passive authentication with a context token. When using active authentication, these cookies must be injected into the request by the application. In CSOM code, this can be accomplished by handling the context object’s ExecutingWebRequest event. This event fires just before the request is sent to SharePoint, giving the application the opportunity to modify the request.

The final step, step 6, is a normal response message from SharePoint with the results of the request. This response is no different when using active or passive authentication. The type of response is determined by the type of request that was made (CSOM, REST, etc.).

Active authentication, also known as remote authentication, is a complex subject. There are several good articles on TechNet, MSDN, and elsewhere, as well as available sample code. Detailing all of these options is beyond the scope of this discussion. The best strategy is to pick a solution, wrap it into a set of common classes or scripts, and reuse that solution throughout your codebase. For more on active authentication, visit the following web sites:

Creating Office Apps

The Cloud App Model described in Chapter 1 applies to more than just apps for SharePoint. Microsoft’s Office productivity suite also supports the use of apps. An app for Office is basically an add-in that appears in the office client application. There are different types of apps for Office that can be surfaced in different members of the Office family of products. Apps for Office can also be used in the web-based versions of the Office Web Apps.

An app for Office is a web application that is designed to run in a small web browser window hosted within the Office application. As web pages, apps for Office can use technologies such as web services, REST, HTML, and JavaScript to provide a rich user experience. Office apps also have the ability, with proper permissions, to read and write data in Office documents and affect the state of the Office application.

In the following sections, we will describe the different types of Office apps, how they are hosted within an Office application, and how they are published to consumers. We will also see how these apps can be integrated into apps for SharePoint to create deep integration between apps for SharePoint and the documents with which they interact.

Anatomy of an App for Office

An app for Office is constructed by creating two components. Like an app for SharePoint, an app for Office uses a standardized XML manifest to describe its contents and properties. The rest of the app is defined by the contents of the web application that implements the logic of the app.

The manifest file for an app for Office is an XML file containing a description of the app and how it interacts with the Office client applications, as shown in Figure 14-2 and Figure 14-3. The General tab is used to set the name and version information. It also defines the permissions required by the app and the types of Office clients in which it can be used. Most important, Source Location contains the URL of the start page for the app. This is the web page that will be loaded into the app’s window within the Office client when the app is launched.

9781430258841_Fig14-02.jpg

Figure 14-2. App manifest designer for Office apps (General tab)

9781430258841_Fig14-03.jpg

Figure 14-3. App manifest designer for Office apps (App Domains tab)

The App Domains tab defines any web domains that may be referenced by the app. If the app attempts to access any domains that are not listed, the app will fail.

The other component of an app for Office is the web content that is displayed in the app’s window. Unlike SharePoint apps, there is only one hosting option for Office apps. Office app web pages must be hosted in a separate web server somewhere on the network. This is similar to a provider-hosted app for SharePoint. The content cannot be hosted within the Office application, analogous to a SharePoint-hosted app for SharePoint, because the Office applications do not contain web servers. Auto-hosting these pages in Azure is not an option because there is no host web with which to associate an auto-hosted site. When an app for Office is launched, it is associated with an Office document and an instance of the Office application, not a SharePoint site. A new Azure site would have to be provisioned every time the app was launched. This would make the provisioning and removal of auto-hosted sites prohibitively expensive.

The requirement for provider-hosted web sites means that most Office apps will require a company’s infrastructure for support. For commercially distributed apps, these servers need to be available on the Internet 24/7. For internally developed apps, web servers for this content will need to be deployed on the organization’s network.

SharePoint 2013 provides a partial answer to this hosting limitation based on the fact that, unlike a client application, SharePoint is a web server. We will explore this type of hosting later in this chapter.

Types of Office Apps

Apps for Office provide an enhanced user experience within one of the Office desktop or web-based applications. There are multiple types of apps that can be created, based on which application(s) will host the app and what type of functionality will be provided.

The first, and most flexible, type of Office app is a task paneapp. A task pane appears beside the office document and provides additional options for the user, as shown in Figure 14-4. A task pane can read and write data in the document and access information over the network. Task panes are flexible because they can be used with any of the document-oriented Office applications that support apps. Note that the task pane is an add-in to the Office application and not a part of the content of the document. It is not saved with the document.

9781430258841_Fig14-04.jpg

Figure 14-4. App for Office task pane app

The next type of Office app is a content pane app(orcontentapp).Content panes are specific to Excel (see Figure 14-5). They provide additional visualization or reference information about the data in the spreadsheet. A content app can be used to embed graphics, text, video, or any other type of web content in the spreadsheet. Instead of appearing to the side of the document, as a task pane does, a content pane is part of the underlying document. When the document is saved, a reference to the app is saved as well. Excel must be able to access the app’s online content when the document is reopened or the content pane will not function. This is important to remember when creating documents using apps in development. Once the Visual Studio debugger is shutdown, the app content is no longer available.

9781430258841_Fig14-05.jpg

Figure 14-5. App for Office contentpane app

The last type of app for Office is a mail app. Mail apps appear next to e-mail messages or calendar items in Outlook, just as task panes do in document-based Office applications(see Figure 14-6). A mail app can access the fields in the current mail or calendar item as well as access information through the network. Because mail apps only work with e-mail and calendar objects, they are only relevant to Outlook 2013 and Outlook Web App.

image Note  In order to use mail apps for Office, your organization must be using either Exchange 2013 or Office 365 for e-mail. While the Outlook 2013 desktop application can connect to other types of mail services, only Exchange supports apps for Office.

9781430258841_Fig14-06.jpg

Figure 14-6. App for Office mailapp

It is important to note which types of apps are supported in which client- or web-based Office applications. Table 14-1 provides a quick reference for which types of apps are supported in which applications.

Table 14-1. Supported Applications for Office Apps

image

Office App Runtime Environment

Because apps for Office run as web applications, they have the same strengths and limitations as web applications running in an IFrame element on a web page. The Office application that hosts the app also imposes limitations that prevent badly behaved apps from impacting the host application.

  • An app can render a rich UI via HTML and JavaScript, but only within the window set aside for it.
  • An app can call web services and REST endpoints.
  • Apps that need to use server-side code can be hosted on a web server supporting it.
  • An app’s web pages are subject to all of the same limitations as ordinary web pages, including security zones and cross-site scripting.
  • Apps for Officeare isolated within a separate runtime environment that enforces read-only or read-write permissions and throttles the resources that can be consumed by an app.

In addition to rendering HTML and calling web services, apps for Office can also access the associated host application through the JavaScript API for Office (http://msdn.microsoft.com/en-us/library/fp142185.aspx). This is a JavaScript library that can be used by the app’s web page to interact with whichever application is hosting it. This library contains members for managing document content, e-mails, contacts, and other application-specific objects. The entry point for these APIs is the Officeobject. This object’sproperties provide access to all of the other objects in the API.

Publishing Office Apps

Publishing apps for Office is very similar to publishing apps for SharePoint. In fact, they use the same storefront: the Office Store.

When a developer or organization offers an app for Office for sale (or for free) on the Office Store web site, the app is validated and signed by Microsoft to ensure its authenticity. The Office Store provides the checkout and download infrastructure for the all of the apps in its library. There is one important difference between apps for Office and apps for SharePoint in the store. An app for Office contains no content, only an app manifest file. Remember, all apps for Office are provider-hosted, so there is no means, or need, to deploy the app’s web content files.

Like SharePoint apps, Office apps can also be deployed within an organization without using the Office Store.

For task pane and content apps, there are two ways to deploy within the organization. The first is to use the same App Catalog that is used for SharePoint apps. You will notice that the standard App Catalog site template contains one library for SharePoint apps and one for Officeapps. Users can acquire apps from either library using the same process we have described before.

Another option is to deploy the XML manifest files for the organization’s apps to a network file share accessible by all relevant users. This rather low-tech option only requires that the file share be designated as a trusted App Catalog in the Office client application. This can be done by the enduser or a centralized IT department.

Mail apps are distributed differently because they only apply to Outlook and Exchange. Exchange 2013 contains its own App Catalog into which these apps can be published. Again, it is only the app’s manifest file that is published. The web content is served from another location on the network.

The last, and most interesting(to us), means of distributing apps for Office is to embed them as part of an app for SharePoint. Content and task pane apps can be created within an app for SharePoint project and invoked automatically. Typically, a document template, such as a Word or Excel file, is included in the app and associated with a document library or custom action in the app’s user interface. The advantage in deploying an app for Office in this way is that it can benefit from SharePoint’s web server infrastructure to deliver the app’s web content. The pages, scripts, and style sheets associated with the app can be included in the app for SharePoint, which will then host them in its app web or remote web as required. There is no need to deploy a separate provider-hosted web server for the app. We will see how to embed an Office app into a SharePoint app in the next exercise.

EXERCISE 14-1

In this exercise, we will demonstrate how to include a simple app for Office into an app for SharePoint. First, we will create an empty SharePoint-hosted app. Then, we will add an Office app and document template to the app. The app web, once deployed, will contain a document library that uses the template to create an Office document that embeds the app for Office.

  • 1.  Open Visual Studio 2012.
  • 2.  Create a new SharePoint-hosted app for SharePoint using the C# template. Name the project SampleSPOfficeApp.
  • 3.  Right-click on the SampleSPOfficeApp project and add a new item.
  • 4.  Select the App for Office item template, as shown in Figure 14-7, and name the item SampleTaskPaneApp.

9781430258841_Fig14-07.jpg

Figure 14-7. New item dialog for App for Office

  • 5.  Click Add.
  • 6.  The next page asks what type of app to create. Note that the Mail app option is grayed out because mail apps cannot be hosted in SharePoint. Make no changes to the dialog and click Next.
  • 7.  The final page offers the option to create a new document template or use an existing one. Leave the create option selected and click Finish.

At this point, the wizard will add several files to the SharePoint app project. The app is added as its own folder containing a default web page, style sheet, and script file. The app’s manifest file is added to the project as well as an OfficeDocuments folder containing a document of each supported type. In our case, this includes a Word document, an Excel spreadsheet, and a PowerPoint presentation.

Now we will add a document library to use these templates.

  • 8.  Right-click on the project and add a new item.
  • 9.  Select List from the item types listed.
  • 10.  Name the list DocsWithApps.
  • 11.  Click Add.
  • 12.  Select Document Library under Create a customizable list template.
  • 13.  Click Next.
  • 14.  On the following page, select the option that reads “Use the following document as the template for this library.”
  • 15.  Click Browse… and then select one of the documents that was created in the OfficeDocuments folder(see Figure 14-8).

    9781430258841_Fig14-08.jpg

    Figure 14-8. Add a document library document template

  • 16.  Click Finish.
  • 17.  Open the Default.aspx file in the editor.
  • 18.  Replace the contents of the div with the following markup:
    <p>
    To view the custom document library <a href="../lists/DocsWithApps">click here</a>.
    </p>
  • 19.  Save and close the Default.aspx file.
  • 20.  Press F5 to start the app.
  • 21.  On the app’s home page, click on the document library link.
  • 22.  Click on the Files tab in the ribbon menu.
  • 23.  Click on the New Document icon to launch a new document, including a task pane app.

At this point, we could add code to the content pages for the app for Office to include additional functionality in the task pane. We could, for example, use the task pane to collect custom metadata about the document that could then be stored in SharePoint.

Creating Windows and WinRT Apps

Windows desktop applications run directly on the Windows operating system without using SharePoint or another application as a host.When accessing SharePoint data, these applications can leverage the same CSOM libraries for the .NET Framework that we have used throughout this book for remote ASP.NET web sites that use server-side code. The difference when creating this type of application is that they do not get the benefit of passive authentication. In order to access SharePoint, they must implement the active authentication protocol described earlier in this chapter.

There are other types of Windows-based applications including Windows services, WCF services, and ASP.NET web sites (not associated with an app for SharePoint). In each of these cases, the same restriction holds true. These types of applications can use the CSOM or REST APIs for SharePoint, but they are responsible for performing their own authentication. As noted earlier, there are sample code libraries on MSDN and CodePlex that provide examples of these techniques.

A new type of Windows application was introduced with Windows 8. These applications run in a new subsystem called the Windows Runtime (WinRT). WinRT was designed to be a stripped-down version of the Windows operating system that could be hosted as part of Windows 8 on a desktop or laptop computer, or on a small tablet device such as the Microsoft Surface RT. The name used to describe these apps has changed over time. Originally introduced as “Metro” applications, they were later renamed to “Windows Store Apps.” As this later name suggests, these apps can be purchased from the Windows Store in the same way that apps for SharePoint and Office can be purchased from the Office Store. We will refer to them as WinRT apps since that is the environment they run in.

WinRT apps are built using a subset of the full .NET Framework called .NET for Windows Store Apps (http://msdn.microsoft.com/en-us/library/windows/apps/br230232.aspx). These apps are typically built in Visual Studio using the MVVM pattern. The classes in the WinRT version of .NET are similar to those associated with the Silverlight subset of .NET. Unfortunately, there is no CSOM library specifically designed for use with WinRT apps (yet). Therefore, WinRT apps that need to access SharePoint will have to use the REST API or SharePoint’s custom web services.

To explore the building of WinRT apps for SharePoint, we are going to use an excellent sample application that is available on CodePlex. The code for this project can be downloaded from http://sharepointwinrt.codeplex.com. When run, this app presents a login screen where the user puts in their SharePoint site URL, user name, and password. It then connects to SharePoint and reads the lists and items available and displays them, as shown in Figure 14-9.

9781430258841_Fig14-09.jpg

Figure 14-9. Sample WinRT application for SharePoint

Obviously, there are many ways to improve this user experience, but the point is that the contents of the SharePoint lists and libraries are available to the app. The project files for this sample app provide a good model implementation of the active authentication process(see Figure 14-10).

9781430258841_Fig14-10.jpg

Figure 14-10. Sample WinRT application project files

The sample solution contains two projects. The first project is a class library called SharePoint.Client, which handles the SAML exchange and SharePoint login messages and cookies. Here are some of the key files in this project:

  • Data/SAML.xml—This XML file is a formatted SAML token request. It contains three locations where substitutions will be made by the app. These are marked with “{n}” in place of the user name, password, and target SharePoint URL.
  • SPLogin.cs—This class manages the SAML login sequence with the Security Token Service and the target SharePoint farm. This class retrieves and stores the authentication cookies from SharePoint.
  • SPConnection.cs—This class is a wrapper for SharePoint’s ListData.svc web service. The requestStreamAsync() method in this class applies the authentication cookies to the HTTP request sent to SharePoint.

The other project in the sample solution is Sharepoint.Companion. This project contains the WinRT app. It is built using the MVVM pattern for its user interface and the SharePoint.Client project for access to SharePoint.

  • App.xaml—This XAML file defines the app’s global user interface properties. Its code-behind file defines event handlers that handle the launch and suspend events. The launch event displays the login page and loads the main page for the app.
  • SampleDataSource.cs—This source file contains multiple classes that implement both the model and view-model objects for the app. The data modeled is the list data from the SharePoint ListData service.
  • SharePointLoginDialog.xaml—This XAML page and its code-behind implement the app’s login form. This form acceptsthe user’s name, password, andSharePoint siteURL. When the user clicks Connect, this page uses the SPLogin class to connect to the SharePoint site.
  • Package.appxmanifest—Like all other types of apps, WinRT apps must have a manifest file. The app manifest defines the app’s name, default language, supported layouts, and icons. It also defines the device capabilities that may be used by the app. This is similar to requesting app permissions.

The remaining files in this project are XAML and code-behind files for the various views displayed in the app.

Creating Windows Phone Apps

Another new platform for apps is the Windows Phone. Windows Phone apps use libraries from the Windows Phone Software Development Kit (SDK). The SDK enables Visual Studio to create and debug Windows Phone apps using a phone simulator instead of a physical Windows Phone.

image Note  Windows Phone apps are specific to Windows Phone. They will not run on iOS or Android-based smartphones.

SharePoint 2013 contains new features that are intended to enrich mobile applications. SharePoint now has built-in field types for storing geographic location information that apps can use to integrate with mapping systems. For example, a retailer could build an app to quickly lookup the closest store to a mobile user by looking up available locations in a list. SharePoint can also generate push notifications to mobile users when data changes or events occur. A user might want to get notifications on their phone when new tasks appear for them in a task list on SharePoint.

To create Windows Phone apps that leverage SharePoint, there is an additional SDK called the Microsoft SharePoint SDK for Windows Phone. This SDK includes new classes to simplify authenticating with SharePoint and building SharePoint-related apps. The SDK also supplies a version of the client-side object model (CSOM) libraries for the Windows Phone platform.

Setting Up the Development Environment

Before creating Windows Phone apps for SharePoint, there is a certain amount of setup required in the developer’s environment.

First, of course, Visual Studio 2012 must be installed. Then, we need to load the correct SDKs. SharePoint 2013 supports the two most recent versions of the Windows Phone platform: version 7.1 and version 8. Table 14-2 provides the URLs for downloading the necessary SDKs for each version. The Windows Phone SDK should be installed first, followed by the SharePoint SDK.

The Windows Phone SharePoint 2013 SDK installs four important assemblies for developing Windows Phone apps for SharePoint.

  • Microsoft.SharePoint.Client.Phone and Microsoft.SharePoint.Client.Phone.Runtime—These libraries contain the client-side object model (CSOM) classes for the Windows Phone platform.
  • Microsoft.SharePoint.Phone.Application—This assembly contains base types for SharePoint fields and lists including model and view-model base classes for these objects.
  • Microsoft.SharePoint.Client.Phone.Auth.UI—This library contains classes that simplify creating the Windows Phone user experience when using SharePoint. It includes the following classes:
    • BrowserLogin—This class is the code-behind for an automated browser login page (BrowserLogin.xaml). This page is shown when login credentials are required.
    • Authenticator—This class greatly simplifies active authentication because it implements the exchange of SAML messages and tokens to authenticate to SharePoint. The result is a client context object that is ready to use.
    • ODataAuthenticator—This version of the authenticator class is designed to work with OData connections that may involve indirect connections to the data service.

The SharePoint SDK for Windows Phone also includes two Visual Studio project templates. These templates provide a starting point for developing apps. These templates will be described in the next sections.

image Note  The Windows Phone project templates for SharePoint are only available in the C# language. No Visual Basic template is included at this time.

Windows Phone Empty SharePoint Application Template

The first template included in the Microsoft SharePoint SDK for Windows Phone is the empty application template. As the name implies, this template contains only the minimal set of files needed to create an app.

This template is basically the same as the empty app template that is included in the Windows Phone SDK. The difference is that this template includes references to the SharePoint SDK assemblies. This template includes a blank main page and an App.xaml file to define the app.

Windows Phone SharePoint List Application Template

The other template included in the SharePoint SDK generates a fully functional application that can read and write SharePoint data.

A common use of mobile apps is to review and update lists of data. Since SharePoint is very good at maintaining lists, it is not surprising that this is a common use for SharePoint apps on mobile platforms. The List Application template was created to provide a quick start to creating this type of app.

The List Application template uses a wizard to connect to an existing SharePoint site, read the views and fields for a list, and generate an app that can perform CRUD operations on the list’s data. The template can read any list to create the app, including document libraries and custom lists. Apps based on lists that include custom field types or complex lookups may need to be modified before being ready for production use.

A list app contains a single project for the Windows Phone app. This project includes several files. The most important of these are the following:

  • App.xaml—This file represents the overall Windows Phone application. The code-behind file associated with App.xamlcontains event handlers for app lifecycle events such as launching, activating, deactivating, and closing of the application.
  • ListDataProvider.cs—This class loads list items from the associated SharePoint list and views.
  • Views Folder
    • List.xaml—This view presents the list items in each of the views selected in the wizard.
    • DisplayForm.xaml—This view presents a read-only form, containing all of the data fields selected in the wizard.
    • EditForm.xaml—This is a data entry form used to update an existing entry in the list.
    • NewForm.xaml—This is a data entry form used to create a new entry in the list.
  • ViewModels Folder
    • ListViewModel.cs—This is the view-model object for the List view.
    • DisplayItemViewModel.cs—This is the view-model object for the DisplayForm view.
    • EditItemViewModel.cs—This is the view-model object for the EditForm view.
    • NewItemViewModel.cs—This is the view-model object for the NewForm view.

In our final exercise, we will use the wizard to create an app that reads and writes data in a SharePoint calendar list.

EXERCISE 14-2

In this exercise, we will walkthrough creating a SharePoint list app using the template included in the Microsoft SharePoint SDK for Windows Phone. This application will display a SharePoint list and allow a mobile user to view and edit its data.

image Note  The assumption is being made here that the developer’s environment has already been updated to include the Windows Phone SDK and Microsoft SharePoint SDK for Windows Phone. The version of the SDKs selected should match the version of the target phone platform.

  1. Log on to the target SharePoint site using an account that has site owner rights.
  2. Create a new list using the Calendar list template. Name the list Assignment Calendar.
  3. Add a handful of events to the calendar to occur over the next few weeks and months.
  4. Open Visual Studio 2012.
  5. Create a new project called SampleSPWinPhoneApp using the Windows Phone SharePoint List Application template (see Figure 14-11).

    9781430258841_Fig14-11.jpg

    Figure 14-11. New project dialog

  6. Click OK.
  7. On step 1 of the wizard, enter the URL for the target SharePoint site and click Find Lists (see Figure 14-12).

    9781430258841_Fig14-12.jpg

    Figure 14-12. List application wizard (step 1 of 5)

  8. Select the Assignment Calendar entry in the list.
  9. Click Next.
  10. On step2, select all of the available views (see Figure 14-13). These will become tabs on the main list page of the app.

    9781430258841_Fig14-13.jpg

    Figure 14-13. List application wizard (step 2 of 5)

  11. Click Next.
  12. On step 3, select all of the available forms. These will become views in the app (see Figure 14-14).

    9781430258841_Fig14-14.jpg

    Figure 14-14. List application wizard (step 3 of 5)

  13. Click Next.
  14. On step 4, select all of the available fields. Note that any required fields are already selected and cannot be unselected (see Figure 14-15).

    9781430258841_Fig14-15.jpg

    Figure 14-15. List application wizard (step 4 of 5)

  15. Click Next.
  16. On step 5, reorder the selected list as desired using the up and down arrows to the right of the list (see Figure 14-16).

    9781430258841_Fig14-16.jpg

    Figure 14-16. List application wizard (step 5 of 5)

  17. Click Finish.

    The wizard will now generate the app in a new project. This app uses the MVVM pattern to create the selected views using the data selected in the wizard.

  18. Press F5 to debug the app using the Windows Phone emulator.
  19. Once the operating systems and app have been loaded into the emulator, you will see a login screen. Enter the user name and password to be used on the site.
  20. Click Sign in.

    Once the app successfully signs in to SharePoint, it reads the list’s data and displays the list view, as shown in Figure 14-17.

    9781430258841_Fig14-17.jpg

    Figure 14-17. List view in the Windows Phone emulator

    The emulator appears on the developer’s desktop in a borderless window. The toolbar to the right of the emulator allows the developer to test different orientations and zoom in on the phone’s display. The ➤ button on the menu opens an additional set of tools that can capture screenshots and test geo-location and accelerometer inputs.

  21. Click on one of the calendar items displayed.
  22. On the display view, click the Edit button on the menu. (Hint: The edit button has a pencil icon.) See Figure 14-18.

    9781430258841_Fig14-18.jpg

    Figure 14-18. Edit form view in the Windows Phone emulator

  23. Change the title or location for the item.
  24. Click the Save button (diskette icon) on the menu bar.
  25. Close the emulator by clicking the X on the emulator’s toolbar.
  26. Open the target SharePoint list in a web browser.
  27. Open the same calendar item and verify that the changes were saved in SharePoint.

The app generated by the list application wizard is complete in most cases. Additional options and changes to the user interface can be implemented by updating the generated classes. These classes are only generated when the project is created, so any changes made later will not be overwritten. Conversely, any changes made to the underlying SharePoint list will need to be reflected in the app’s source code manually.

Summary

In this chapter, we have moved beyond building apps for SharePoint to building distributed enterprise-class solutions on various platforms. While we have focused on using Microsoft’s tools and platforms, these techniques are equally applicable to any web-based application stack. The key is the authentication sequence. The exchange of tokens and cookies is what allows an external application to use SharePoint resources. That protocol can be implemented on any platform that supports HTTP.

We hope that you have enjoyed learning about building apps for SharePoint using the Cloud App Model. There are many opportunities for companies and individuals to use these techniques to create robust, highlyfunctional solutions. We wish you good luck in finding ways to extend the SharePoint platform both within your organization and in the cloud.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.17.40