Using CSOM in a Windows 8 app for SharePoint Online

Working with the Client-Side Object Model is rather easy and that’s why I looked for a solution to use it in a Windows 8 app.

There are two assemblies to be referred for working with the CSOM.

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll

Creating the app

Let’s start by creating a new Class Library for Windows Store apps.
In here we add the references to the SharePoint assemblies.
In our Windows 8 app, we add a reference to our newly created class library.

Untitled

To be able to invoke methods on SharePoint Online using the Client-Side Object Model (CSOM), we need to authenticate first.
The authentication is based on this solution found on MSDN Blogs:
http://blogs.msdn.com/b/omarv/archive/2012/10/25/windows-8-store-apps-office-365-enterprise-preview-sharepoint-online.aspx

When the app starts, the app will try to retrieve the credentials from the password vault to connect to SharePoint. If the credentials from the password vault are empty, you’ll get the input fields to pass your credentials and login.

After the login is done, the cookies are stored in a CookieContainer. We need to pass this cookies to the CSOM ClientContext.

context.ExecutingWebRequest += delegate(object sender, WebRequestEventArgs e)
{
    e.WebRequestExecutor.WebRequest.CookieContainer = Cookies;
};

Now that we are loged in and that the cookies are passed to the ClientContext, we can do what ever we want using CSOM code.

In this example we will retrieve the site title.

BasicPage

SPClient client = new SPClient();
client.SiteUrl = AuthUtility.Current.SiteUrl.ToString();
client.Cookies = AuthUtility.Current.CookieContainer;
TextBlockTitel.Text = client.GetSiteTitle();

Class using the CSOM

public class SPClient
{
	public string SiteUrl { get; set; }
	public CookieContainer Cookies { get; set; }

	public string GetSiteTitle()
	{
		string Title = string.Empty;
		ClientContext context = null;

		try
		{
			using (context = new ClientContext(SiteUrl))
			{
				try
				{
					context.ExecutingWebRequest += delegate(object sender, WebRequestEventArgs e)
					{
						e.WebRequestExecutor.WebRequest.CookieContainer = Cookies;
					};
				}
				catch
				{
					if (context != null) context.Dispose();
					throw;
				}

				context.Load(context.Web);
				context.ExecuteQuery();
				Title = context.Web.Title;
			}
		}
		catch
		{
			throw;
		}

		return Title;
	}
}

You can download the sample code here. Enjoy!

Connect and use a web service in windows phone apps

This article explains how to connect to any web service and use it with a windows phone application. In my example I used this public weather SOAP web service.
For more public open web services: http://www.service-repository.com/

So first we have to add a service reference to the web service.

Now that we have our service reference, we can initialize a new client. Afterwards we can call any web method from this service client.

ServiceReference.WeatherSoapClient client = new ServiceReference.WeatherSoapClient();

All calls from windows phone applications to services are always asynchronous. So the method names will look like ‘GetMethodAsync‘. In the example we’ll use the method GetCityWeatherByZIPAsync with parameter value 18001.

client.GetCityForecastByZIPAsync("18001");

At certain time after calling the method, the data will return. At that point the method completed event will be raised and we have to react to it with an EventHandler. But this handler has to be set up before the call is made.
So you have to put this line of code before the async call.

client.GetCityWeatherByZIPCompleted += new EventHandler<ServiceReference.GetCityWeatherByZIPCompletedEventArgs>(client_GetCityWeatherByZIPCompleted);

The handler method will return the result of the service. That result is in the EventArgs variable e.

void client_GetCityWeatherByZIPCompleted(object sender, ServiceReference.GetCityWeatherByZIPCompletedEventArgs e)
{
      MessageBox.Show(String.Format("Temperature: {0} °F", e.Result.Temperature));
}