SharePoint Saturday The Conference DC - How the client object model saved the day


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

SharePoint Saturday The Conference DC - How the client object model saved the day

  1. 1. How the Client ObjectModel Saved the Day!Liam ClearySolution Architect | SharePoint MVP
  2. 2. About Me• Solution Architect @ SusQtech (Winchester, VA)• SharePoint MVP since 2007• Working with SharePoint since 2002• Worked on all kinds of projects • Internet • Intranet • Extranet • Anything SharePoint Really• Involved in Architecture, Deployment, Customization and Development of SharePoint
  3. 3. It is a wise man who knowswhere courage ends andstupidity begins.Jerome Cady – Hollywood Screenwriter
  4. 4. Agenda• Data Access In SharePoint • SharePoint 2007 • SharePoint 2010• What is the Client Object Model? • Managed Code • Silverlight • ECMAScript• How can we use it?• How did it save the day?• NOTE: Not a heavy code session, well we will see!!
  5. 5. Data Access In SharePoint 2007• Historically we were tied to direct API or Web Services SharePoint Database SharePoint API Server Applications Web Services (Native / Custom) Client Applications
  6. 6. Problems• Out of the box Web Services may not have the required methods• Custom Web Services can be very complicated to create• API is not rich enough • Custom API wrappers are created • Seen this again and again• Complicated to all from JavaScript / jQuery• Client Applications if developed are all custom, even data access• Applications are not really developed• See the same issues with every client
  7. 7. Data Access In SharePoint 2010• Updated for SharePoint 2010 • Similar Approach with a few tweaks SharePoint Database SharePoint API Web Services Client Object Model (Native / Custom) Client Applications Server Applications
  8. 8. What is the Client Object Model?• “Knight in Shining Armor” for client application development• Simple and easy to use API• Consistent implementations• Abstracted methods from core API • Subset of the Types and Members from Microsoft.SharePoint namespace• Three flavors • .NET CLR • Silverlight • ECMAScript (JavaScript, Jscript)• Easy to Consume data from SharePoint
  9. 9. Client Object Model Browser Based ECMA Script (JavaScript) ECMA Script Object Model SharePoint Server Object Model Proxy Client Services Client Proxy SQL Server Content Databases Managed Object Model .NET Managed (.NET & Silverlight)
  10. 10. Client Object Model – Process• Data is retrieved in a specific way Client Application Server Commands Client.svc XML context.ExecuteQuery(); Commands JSON Process Results Send Back Results
  11. 11. Client Object Model - Objects • Similar to core API • Naming slightly changed • Consistent across all implementationsServer .NET Managed Silverlight JavaScript(Microsoft.SharePoint) (Microsoft.SharePoint.Client) (Microsoft.SharePoint.Cliernt.Silverlight) (SP.js)SPContext ClientContext ClientContext ClientContextSPSite Site Site SiteSPWeb Web Web WebSPList List List ListSPListItem ListItem ListItem ListItemSPField Field Field Field
  12. 12. Client Object Model – Getting Started• No ClientContext = No ConnectionclientContext = new ClientContext(“”);• Need to LOAD before you can READclientContext.Load(web);clientContext.Load(web.Lists);• Must COMMIT requests in a BATCHclientContext.ExecuteQuery();clientContext.ExecuteQueryAsync(succeedcallback, failurecallback);
  13. 13. Client Object Model – Object Identities• Used to batch up objects that can be used before the “ExecuteQuery”ClientContext clientContext = new ClientContext(“http://siteurl”);List list = clientContext.Web.Lists.GetByTitle(“ListName”);CamlQuery camlQuery = new camlQuery();camlQuery.ViewXml = “<View/>”;ListItemCollection listItems = list.GetItem(camlQuery);clientContext.Load(list);clientContext.Load(listItems);clientContext.ExecuteQuery();• Once the objects have been set and executed they can be iterated throughforeach(ListItem listItem in listItems) Console.WriteLine(“Title: ,1-”, listItem*“Title”+,
  14. 14. Client Object Model – Lambda Expressions• Used trim results, filter or even increase performance Trimming ClientContext clientContext = new ClientContext("http://siteurl"); Web site = clientContext.Web; clientContext.Load(site, s => s.Title, s => s.Description); clientContext.ExecuteQuery(); Filter ClientContext clientContext = new ClientContext("http://siteurl"); ListCollection listCollection = clientContext.Web.Lists; IEnumerable<List> hiddenLists = clientContext.LoadQuery( listCollection . Where(list => !list.Hidden && list.BaseType == BaseType.DocumentLibrary)); clientContext.ExecuteQuery(); Performance ClientContext clientContext = new ClientContext("http://siteurl"); IEnumerable<List> lists = clientContext.LoadQuery( clientContext.Web.Lists.Include( list => list.Title, list => list.Hidden, list => list.Fields.Include( field => field.Title, field => field.Hidden))); clientContext.ExecuteQuery();
  15. 15. Client Object Model – Asynchronous Processing• Ability to invoke intensive queries Asynchronously delegate void AsynchronousDelegate(); Public void Run() { ClientContext clientContext = new ClientContext("http://siteurl"); ListCollection lists = clientContext.Web.Lists; IEnumerable<List> newListCollection = clientContext.LoadQuery( lists.Include( list => list.Title)); AsynchronousDelegate executeQueryAsynchronously = new AsynchronousDelegate(clientContext.ExecuteQuery); executeQueryAsynchronously.BeginInvoke( arg => { clientContext.ExecuteQuery(); foreach (List list in newListCollection) Console.WriteLine("Title: {0}", list.Title); }, null); }
  16. 16. Client Object Model - .NET• Provides easy access from remote .NET Clients to SharePoint Data• Can be used from Managed Code such as Office• Utilizes the following Assemblies • Microsoft.SharePoint.Client.dll (281 KB) • Microsoft.SharePoint.Client.Runtime.dll (145 KB)• Compared to Microsoft.SharePoint.dll (15.3 MB)• SQL Like• Batch Processing
  17. 17. Client Object Model – Managed CodeDEMO
  18. 18. Client Object Model - Silverlight• Use Silverlight on page or in a web part• Web Part can contain custom properties by using the “InitParams” property• XAP file deployed to Layouts or Content Database• Once the Silverlight is loaded it can access the Client Object Model• Stored in the “14TEMPLATELAYOUTSClientBin” directory• Utilizes the following Assemblies • Microsoft.SharePoint.Client.Silverlight.dll (262 KB) • Microsoft.SharePoint.Client.Silverlight.Runtime.dll (138 KB)• Must call “clientContext.ExecuteQueryAsync”
  19. 19. Client Object Model – Silverlight (WPF)DEMO
  20. 20. Client Object Model - ECMAScript• Page needs to load the “SP.js” • Use <SharePoint:ScriptLink>• Can use debug version • Use <SharePoint:ScriptLink …ScriptMode=“Debug”>• Client Context can be set using var clientContext = new SP.ClientContext.get_current(); Or var clientContext = new SP.ClientContext();• SAVE TIME NOTE: Properties are case sensitive• SP.js (381 KB), SP.Debug.js (561 KB)
  21. 21. Client Object Model – ECMAScriptDEMO
  22. 22. Client Object Model – Wrap-up• .NET CLR has a Sync Method whereas Silverlight CLR and JavaScript are Asynchronous• All requests are throttled, so be aware of performance• No ELEVATION of privilege capabilities • SPSecurity.RunWithElevatedPrivileges• Must handle the Synchronize and Update logic• Need to handle the efficient loading etc. of objects • Use LINQ • Use Lambda Expressions• Works well however an element of developer experience is needed to use
  23. 23. Client Object Model – How did it save the day?• Quickly update multiple items on SharePoint • No direct Server Access • Using Forms Login also• Add “cool” functionality easily using ECMA Script • Inline editing• Able to track my wifes spending via SharePoint • Used Client Object Model to remotely check SharePoint list for expenditure and alert on desktop• Completely made up, but the reality is that simple, this could be done, as long as the data feed is available this is achievable
  24. 24. Thank You• Personal Email:• Work:• Twitter: @helloitsliam• Blog: