Monday, April 01, 2013

Building a Windows 8 app for SharePoint 2013 Part 1: using Javascript and CSOM

I followed two sessions about building a Windows 8 app for SharePoint 2013 during the SharePoint conference in Vegas. The two sessions used a different approach for building such an app. In the next couple of blog posts I will outline the different approaches and provide some feedback around it. Let’s start with the CSOM approach.

The SharePoint CSOM and Javascript approach to build Windows 8 apps
During the session Fun with SharePoint Social, CSOM and Windows 8 delivered by Mark Rackley and Eric Harlan (Slideshare for a local BIWUG redelivery available here) at SharePoint Conference Las Vegas a solution depicted by the image below was built. For a complete walkthrough check out Creating your First Windows 8 Application using the Client Object Model (CSOM) and JavaScript
from Mark.

For those of you unfamiliar with CSOM – this is an abbreviation of the  SharePoint Client Side Object Model. So the basic setup was actually a Visual Studio solution which combines a JavaScript WinRT project and a Windows Runtime compontent.  Listed below is  a download link with a  very simple example to get you started.  This is how the code of the Windows Runtime Component looks like – it is basic CSOM code.  Some things you should think about:
  • Add a reference to the required Microsoft SharePoint Client Runtime components
  • Make sure that you pass in the required credentials – the code below assumes that the Windows 8 machine is in the same domain as your SharePoint Server and that you can log in with integrated security
  • The SocialFollowingManager is the most important class – for background info on follows in SharePoint 2013 – check out Common programming tasks for following content in SharePoint 2013
  • You will need to use a separate object to pass the information from the SocialFollowingManager back to the Javascript Windows Store App. By default the SocialFollowingManager returns a ClientResult of type SocialActor which is not a valid Windows Runtime Parameter type and which can’t be returned in a public method
  • You can’t use nested classes – that’s whyFollowedContent is a separate class.
  • If you receive “Unable to connect to the remote server” when connecting to SharePoint you probably still need to make sure that your application has the correct capabilities enabled.  Open up your Application Manifest and make sure that you have “Enterprise Authentication” and  “Private Networks (Client and Server) checked

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Threading.Tasks;
  6: using System.Net;
  7: using Microsoft.SharePoint.Client;
  8: using Microsoft.SharePoint.Client.Social;
 10: namespace CSOM
 11: {
 12:     public sealed class FollowedContent
 13:     {
 14:         public string ID { get; set; }
 15:         public string Type { get; set; }
 16:         public string Name { get; set; }
 17:         public string Uri { get; set; }
 18:     }
 20:     public sealed class Social
 21:     {
 23:         public static FollowedContent[] GetFollowedContent()
 24:         {
 26:             ClientContext ctx = new ClientContext("http://sp2013");
 27:             ctx.Credentials = System.Net.CredentialCache.DefaultCredentials;
 29:             SocialFollowingManager sfmgr = new SocialFollowingManager(ctx);
 30:             ClientResult<SocialActor[]> actors = sfmgr.GetFollowed(SocialActorTypes.All);
 32:             sfmgr.Context.ExecuteQuery();
 34:             FollowedContent[] followedContent = new FollowedContent[actors.Value.Length];
 36:             int index = 0;
 38:             foreach (SocialActor actor in actors.Value)
 39:             {
 40:                 FollowedContent content = new FollowedContent();
 41:                 content.ID = actor.Id;
 42:                 content.Type = actor.ActorType.ToString();
 43:                 content.Name = actor.Name;
 44:                 content.Uri = actor.Uri;
 45:                 followedContent[index++] = content;
 47:             }
 49:             return followedContent;
 50:         }
 51:     }
 52: }

I like this approach of building Windows 8 apps since it allows for a clean separation of the SharePoint codebase from the UI part. At the same time SharePoint developers can leverage the SharePoint object model using CSOM.  It is possible to leverage numerous Javascript libraries (such as JQuery UI) to build a compelling user experience.

Code samples – download BIWUGApp1.rar

No comments: