WINDOWS PHONE

Like with the .NET managed code and JavaScript CSOMs, CSOM libraries are available for both Windows Phone and Silverlight. This availability allows developers to use the same common library and patterns to integrate Windows Phone mobile apps in almost the same way they would in a JavaScript-based SharePoint-hosted application. The reason mobile apps are not exactly the same is because a developer must take into consideration a few additional things in mobile scenarios that are not as prevalent in other types of applications. For example, you must think about scenarios where the phone is not connected to a data network. Will it support offline data sync? What about on different types and speeds of data network? How do you connect to the SharePoint Server while you are not on the same network and possibly behind a firewall? Finally, what about your authentication options? These are all things you must consider and design for in your mobile applications.

The Windows Phone CSOM assists you with some of these issues; however, many others are up to you as a developer to work out how you want to support them. The Windows Phone CSOM offers support for new authentication options by supporting forms authentication, basic authentication, and Office 365 authentication. Previously, there wasn’t a CSOM library that would run on Microsoft’s Windows Phone, and communication was limited to Web service calls. Additionally, authentication was very hard, nearing impossible for many. Additional products that facilitated transforming forms authentication to Windows authentication put it out of reach for most. The new Windows Phone CSOM solves this problem by wrapping each of the authentication options for you so that you don’t have to do the plumbing work. However, they still might offer some challenges such as not supporting Windows authentication (NTML), which many on-premises SharePoint implementations rely on. However, the authentication options that are supported certainly cover many of the scenarios that are more broadly applicable in mobile scenarios where the user is out of the office and connecting over the Internet.

Additionally, the Microsoft SharePoint SDK for Windows Phone 7.1 adds support in Visual Studio 2012 for SharePoint mobile project templates that allow you to get building applications faster with some convenient templates you can extend and build on.

The improvements offered for Windows Phone mobile applications with the 2013 release include:

  • Visual Studio templates to get you started
  • SharePoint Client-Side Object Model support
  • Extended authentication support (forms, basic, Office 365)

Setup

To start building Windows Phone applications that communicate with SharePoint you need the following components:

  • Visual Studio 2010
  • Windows 7 or Vista
  • A SharePoint Server on premises or an Office 365 site
  • SharePoint SDK for Windows Phone 7.1
  • Windows Phone SDK 7.1

NOTE SharePoint does not support being run on a Windows 7, Windows 8, or Vista operating system. If you don’t have access to another machine running SharePoint 2013 then a good option is to sign up for Office 365.

After you have the requisite applications and SDKs installed you can find the CSOM libraries for referencing in the following:

%ProgramFiles (x86)%Microsoft SDKsSharePointv15.0Phonev7.1Libraries

This directory includes the three main DLLs you need:

  • Microsoft.SharePoint.Client.Phone.dll
  • Microsoft.SharePoint.Client.Phone.Runtime.dll
  • Microsoft.SharePoint.Phone.Application.dll

Much like the Managed CSOM, you must reference these DLLs in your application to get access to the CSOM types required.

You may also start your project from one of the provided Visual Studio templates:

  • The Windows Phone empty SharePoint application
  • The Windows Phone SharePoint List application

These templates, by default, reference the required CSOM assemblies for you. The empty application, as its name suggests, gives you a blank canvas to create your application, whereas the list application template lays down some starter files to get you started building an app that reads and saves data in a SharePoint list. It walks you through a series of steps in a wizard that allows you to pick your SharePoint site, list, and fields with which you want to interact and then builds the appropriate views and view models using a Model-View-ViewModel (MVVM) design pattern (see Figure 9-6).

Querying

The only major difference between the Windows Phone CSOM and the Managed CSOM that you will not be familiar with from previous releases is in how you set up authentication. This step is required prior to making any CSOM calls. To do it you first pick the authentication option that your SharePoint site uses. You create a ClientContext object and then set up the Credentials property on it. In the following example the Credentials property is set to authenticate using Office 365:

ClientContext context = new ClientContext("https://contoso.sharepoint.com/");
Authenticator auth = new Authenticator();
 
auth.AuthenticationMode = ClientAuthenticationMode.MicrosoftOnline;
context.Credentials = auth;

NOTE The Windows Phone CSOM library assists your authenticating with SharePoint Online by opening a Sign In window that allows the user to sign in to the SharePoint site, as shown in Figure 9-7. Providing a username and password and circumventing this user interaction are not possible at this time.

Alternatively, if your SharePoint Server uses forms authentication, then you can supply a username and password as follows:

auth.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
 
auth.UserName = "Your_UserName";
auth.Password = "Password";

After you indicate your authentication options you are ready to start making CSOM calls to SharePoint. The code is almost the same as what you would use with the JavaScript or Managed CSOM; however, because Windows Phone Silverlight requires all network calls to be made asynchronously, you must cater callbacks in that manner for when your CSOM call succeeds or fails. To illustrate this process the following code makes a CSOM call to retrieve a list from the SharePoint Server:

List list = context.Web.Lists.GetByTitle("My Custom List");
 
// Load the query and execute the request to fetch data.
context.Load(list);
 
context.ExecuteQueryAsync((object obj, ClientRequestSucceededEventArgs args) =>
    {
        // success
        var SiteId = list.Id;
    },
    (object obj, ClientRequestFailedEventArgs args) =>
    {
        // query failed.  
    });

As you can see the code defines a success and a failure function inline using a lambda expression. In your success function you can query and use the now “full” objects you asked for in your CSOM query.

The Windows Phone Silverlight Client-Side Object Model provides a great set of functionality to quickly get you up and running querying and interacting with SharePoint. You must take into account some differences with authentication and some runtime subtleties such as asynchronous callbacks. However, for the most part you can use the CSOM the same way you use the Managed CSOM.

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

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