Tags

, , , ,

Getting data from SharePoint Online to Windows phone is not too complicated. But I am referencing here only projects targeting Windows 8.0, as Microsoft supply a SharePoint SDK only for this platform and I am not aware yet of a SDK targeting Windows 8.1.

As a concrete example, I have created an announcement list called “Company News” in a SharePoint Online environmental and I trying to display it in a client mobile application. The structure of announcement list is simple, being only interested in getting only 3 columns: Title (single line of text), Body (plain text), and Category (choice field).

I will start presenting structure of my project, which is a Silverlight blank application for Windows phone, and explain it step by step.

Sprint Structure

CompanyUpdate.cs is a simple class representing data structure of my SharePoint list. A list of CompanyUpdate objects will be used as source for a longlistselector control.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SPRInt
{
    class CompanyUpdate
    {
        public string Title { get; set; }
        public string Category { get; set; }
        public string Body { get; set; }

    }
}

ExtensionMethods.cs contains a single static class with a single extension method, designed to replace br tags with /n and strip all other HTML tags. That’s because content of Body column contains HTML tags but my application is not HTML based.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SPRInt
{
     public static class ExtendedMethods
    {
         public static string StripTags (this string input)
         {
             string _input = Regex.Replace(input, @"(<br */>)|(\[br */\])", "\n");
             _input = Regex.Replace(_input, @"(<.*?>)", String.Empty;
             return _input;
         }

    }
}

We are moving now to ListOperations.cs , which is actually the core of the application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Phone;

namespace SPRInt
{
    class ListOperations
    {
        string url;

        /// <summary>
        /// List operations constructor
        /// </summary>
        /// <param name="url"></param>
        public ListOperations(string url)
        {
            this.url = url;
        }

        /// <summary>
        /// Get list items
        /// </summary>
        /// <param name="listName"></param>
        public void GetItems(string listName, Action<List<CompanyUpdate>> callback)
        {
            Authenticator sprintAuthenticator = new Authenticator();
            sprintAuthenticator.AuthenticationMode = ClientAuthenticationMode.MicrosoftOnline;
            sprintAuthenticator.CookieCachingEnabled = true;

            ClientContext ctx = new ClientContext(this.url);
            ctx.Credentials = sprintAuthenticator;

                List spList = ctx.Web.Lists.GetByTitle(listName);
                CamlQuery query = CamlQuery.CreateAllItemsQuery();
                ListItemCollection items = spList.GetItems(query);
                ctx.Load(items);

                ctx.ExecuteQueryAsync(
                    (Object sender, ClientRequestSucceededEventArgs args) => {
                        List<CompanyUpdate> companyUpdates = new List<CompanyUpdate>();
                        foreach (ListItem item in items)
                        {
                            CompanyUpdate cUpdate = new CompanyUpdate();
                            cUpdate.Title = Convert.ToString(item["Title"]);
                            cUpdate.Category = Convert.ToString(item["Category"]);
                            cUpdate.Body = Convert.ToString(item["Body"]).StripTags();
                            companyUpdates.Add(cUpdate);
                        }
                        callback(companyUpdates);
                    },
                    (Object sender, ClientRequestFailedEventArgs args) => {

                    }
                );

        }

    }
}

You can note I am using Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Phone namespaces in the file, these being available because I have SharePoint SDK for Windows phone libraries to my project. Being now able to use classes coming from these namespace, I can make use of Authenticator class (responsible to authenticate me with SharePoint Online) and other client object model classes. Class is instantiated with url of our SharePoint and has a method called GetItems which accepts two parameters: the name of the list and an action which serves as callback method.

It remains MainPage.xaml and associated cs file. There is nothing complicated with this file. It is a simple Silverlight page containing a LongListSelector. When page is loaded, data is getting from the SharePoint list and displayed into the LongListSelector.

<phone:PhoneApplicationPage
 x:Class="SPRInt.MainPage"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
 xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d"
 FontFamily="{StaticResource PhoneFontFamilyNormal}"
 FontSize="{StaticResource PhoneFontSizeNormal}"
 Foreground="{StaticResource PhoneForegroundBrush}"
 SupportedOrientations="Portrait" Orientation="Portrait"
 shell:SystemTray.IsVisible="True" Loaded="PhoneApplicationPage_Loaded">

 <!--LayoutRoot is the root grid where all page content is placed-->
 <Grid x:Name="LayoutRoot" Background="Transparent">
 <Grid.RowDefinitions>
 <RowDefinition Height="Auto"/>
 <RowDefinition Height="*" />
 </Grid.RowDefinitions>



 <!--TitlePanel contains the name of the application and page title-->
 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,12,28">
 <TextBlock Text="SPRInt for mobile" Style="{StaticResource PhoneTextNormalStyle}" />
 <TextBlock Text="News" Style="{StaticResource PhoneTextTitle1Style}"/>
 
 </StackPanel>

 <!-- Data displayed here -->
 <phone:LongListSelector Grid.Row="1" Name="lstCompanyNews" Margin="12,0,12,0">
 <phone:LongListSelector.ItemTemplate>
 <DataTemplate>
 <StackPanel Orientation="Vertical">
 <TextBlock Text="{Binding Title}" Style="{StaticResource companyupdatetitle}" />
 <TextBlock Text="{Binding Category}" Foreground="AliceBlue" />
 <TextBlock Text="{Binding Body}" Foreground="AliceBlue" />
 </StackPanel> 
 </DataTemplate>
 </phone:LongListSelector.ItemTemplate>

 </phone:LongListSelector>


 </Grid>

</phone:PhoneApplicationPage>
       
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Phone;
using Microsoft.Phone.Shell;

using SPRInt.Resources;

namespace SPRInt
{
 public partial class MainPage : PhoneApplicationPage
 {
 // Constructor
 public MainPage()
 {
 InitializeComponent();
 }

 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
 {
 ListOperations ops = new ListOperations("https://sprint");
 Action<List<CompanyUpdate>> callback = (List<CompanyUpdate> items) =>
 {
 this.Dispatcher.BeginInvoke(() =>
 {

 this.lstCompanyNews.ItemsSource = items;
 });
 };
 ops.GetItems("Company News", callback);

 }

 }
}
Advertisements