jump to navigation

Working with the new Client Object Model: From SPC09 Las Vegas October 20, 2009

Posted by willhlaw in jQuery, SharePoint, Sharepoint Designer, SPC09.
Tags: , , , ,
9 comments

Live from Las Vegas at Mandalay Bay. I am at the SharePoint Conference 2009 and listening to a presentation on the new SharePoint 2010 Client Object Model. Presenter was Paul Stubbs from Microsoft.

Client Object Model

  • Simple API to Add, Retrieve, Update and Manage Date in Shareponit.
  • 1st goal – consistency
    • Between client and server API
    • As well as between the three different clients, Client side (.NET), Silverlight, and Javascript.
  • 2nd goal – efficiency
    • In use of server resources
    • In use of network resources
    • Uses a SQL model. Batch up client calls, call server, server processes query, server turns results as a result set
    • Only return data the exact data that I need. i.e. When calling a list, only retrieve data for the specific columns that are needed.
  • Code example
    • Today:
      • SPWeb site = SPContext.Current.Web;
      • String title = site.Title;
      • Site.Title = title + ” and Client OM”;
      • Site.AllowUnssafeUpdates = true;
      • Site.Update();
    • Now with Client Object Model:
      • ClientContext = clientContext = new ClientContext(http://intranet.contoso.com);
      • Web site = clientContext.Web;
      • clientContext.Loadsite);
      • clientContext.ExecuteQuery(); //synchronous call
      • site.Title += ” and Client OM”;
      • site.Update((); //second call because as a rule, can’t use a property to set the value of another property.
      • clientContext.ExecuteQuery();
    • Another example:
      • Web site;
      • ClientContext ctx;
      • Void MainPage_Loaded(object sender, RoutedEventArgs e)
      • { contexzt = new ClientContext(http://mysite);
      • Site = context.Web;
      • Context.Load(site);
      • //Call the Sharepoint Server
      • Context.ExecuteQueryAsync(succeededCB, failedCB);}
    • Javascript example:
      • _spBodyOnLoadfunctionNames.push(“Initialize”);
      • Var site;
      • Var Context;
      • Function Initialize()
      • { contexst = SP{.Clientcontext.get_current();
      • Site = Context.get_web();
      • Context.load(site, “Title”);
      • //Call the Sharepoint Server
      • Context.executeQueryAsync(Suceeded, Failed);}
  • Retrieve Data
    • Context.Load(object, params LinqExpre3ssion)
      • Fills out the objects in the context; in-place
      • ‘method syntax’ linq only
  • Context.Loadquery(iQueryable)
    • Use linq query to return custom objects
    • Not filled into the client context
    • Both ‘query syntax’ and ‘method syntax’ linq
  • Query Syntax
    • More natural Linq query that is familiar
    • Var query = from list in clientContext.Web.Lists where list.Title != null select list;
  • Method Syntax
    • More technical
    • clientContext.Load(oList, list => list.Fields.Where( field => field.hidden === false && field.Filterable == true));
    • Harder, but more powerful.
  • How does this all work? How to access data with Client OM?
    • A new service called Client.svc is a proxy to WCF service.
    • When .ExecuteQuery occurs, block of XML gets sent to server, it unwinds it, and returns JSON object to client.
    • Use fiddler to see the payload to understand how it works, but once you do, can ignore the traffic details.
  • Demo 1, change site title “Hello World”:
    • Shows an example of client.svc runtime being used in a windows application to change the title of a site.
  • Client Object Model using .NET is synchronous, but Silverlight and Javascript are asynchronous.
  • Demo 2, a WPF example:
    • Purpose is to fill a list box with a List of Lists.
    • There are some namespace collisions when building the WPF app.
    • So, you must write: Using SP = Microsoft.Sharepoint.Client again to prevent namespace conflict.
    • Object model will turn query syntax into method syntax before sending it to the server. Remember, query syntax is easier to write.
  • Demo 3, showing .NET CLR in Word
    • Drop .NET controls on the canvas onto the design surface within Visual Studio.
    • Purpose is to fill a task pane (Action Pane, uses ElementHost control because it needs to be Winforms based) on the right-hand side of customer data.
    • Also showing grabbing fields from a list.
  • Demo 4, javascript and jQuery
    • In visual studio, starting a new OMJavascriptApplication
    • <SharePoint:scriptLink> name=”sp.js”. -> Name of javascript client object model
    • <script src = js/jquery-ui-1.7.1.custom.min.js and other jquery files. Presenter mentions that trontastic is a great css to use. I have to check it out.
    • Bui8lding a dynamic jQuery Accoridion on the page by setting a div id=”accordion”
    • Has a separate MyJScode.js file
      • Inside the file, uses _spBodyOnLoadfunction to push Initialize function with familiar code that gets context, etc.
      • LoadListBox is pure jQuery code to populate listbox with a change event.
      • Var listEnumerator = lists.getenumerator();
      • While listEnumerator.moveNext()) {
      • }
      • Then listbox1.append using standard jquery.
      • BuildTable(ListName) is pure jQuery code.
    • Purpose of example is to show List of Lists inside the accordion! Really cool. This caused the audience to clap for the first time in the entire demo!
  • Javascript Client OM
    • Javascript files are in the 14 bin folder on the server.
    • There is a debug version (SP.debug.js)
    • SP.Core.js and SP.Core.debug.js
    • SP.Runtime.js (SP.Runtime.debug.js)
    • Debug versions are only slighter larger.
  • Next demo, Silverlight example
    • In visual studio, starting a new OMSilverlightApplication in Visual Studio
    • Starts with a simple grid with listbox
    • Using SP = Microsoft.Sharepoint.Client again to prevent namespace conflict.
    • Can build using standard Silverlight code to run in browser or inline, which I think means stand alone on the client machine.
    • It is easy to go from WPF code to Silverlight using this.Dispatcher.BeginInvoke and using a lambda expression.
      • i.e. this.Dispatcher.BeginInvoke ( ()=> {messageBox.Show(string.Forma(“failed – msg”); })
    • There are lot of steps to publish the Silverlightproject. Seems like several options for outputting the project. Grant it, I am not familiar with Silverlight at all.
    • (uh-oh, big clapping from next session over!) Yeah Microsoft.
    • When deploying Silverlight app to Sharepoint, a Silverlight.xap file is published to a Silverlight folder located at
    • The project does not exist on the SharePoint server, it is deployed as a sandbox solution.
    • In site settings -> solutions -> OMSilverlightApplicatinoDeploy exists for a site collection administrator to deploy.
    • Since deployment option was checked to allow Silverlight webapp to be installed on client, you can right-click the Silverlight listbox app, and can install the app on the client!
    • (This got the second clap of the session from the audience!)
  • No reason to deploy in WPF (if you are not doing full trust or 3D). To have updates, click once, security, and to have offline capability, build apps in Silverlight!    
  • #SPC09Buzzword for this session, “Super Powerful. Super Powerful”
  • Silverlight CLR Client OM
    • Located in 14 bin on server.
    • Microsoft.SharePoiht.Client.Silverlight (262KB)
    • Microsoft.SharePoint.Client.Silverlight.Runtime (138KB)
  • Q & A
    • I asked what recommended methods does Microsoft have for deloying jQuery and script libraries to Sharepoint Pages. There was no real recommendation other than including it in masterpage globablly or referencing it in a single .aspx file, but Scott was the second time I heard to talk with Mike Amerland.
Advertisements