Digital Identity is Under Attack: FIDO Paris Seminar.pptx
SharePoint Saturday The Conference DC - How the client object model saved the day
1. How the Client Object
Model Saved the Day!
Liam Cleary
Solution Architect | SharePoint MVP
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. It is a wise man who knows
where courage ends and
stupidity begins.
Jerome Cady – Hollywood Screenwriter
4.
5. 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!!
6. 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
7. 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
8. 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
9. 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
10. 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)
11. 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
12. Client Object Model - Objects
• Similar to core API
• Naming slightly changed
• Consistent across all implementations
Server .NET Managed Silverlight JavaScript
(Microsoft.SharePoint) (Microsoft.SharePoint.Client) (Microsoft.SharePoint.Cliernt.Silverlight) (SP.js)
SPContext ClientContext ClientContext ClientContext
SPSite Site Site Site
SPWeb Web Web Web
SPList List List List
SPListItem ListItem ListItem ListItem
SPField Field Field Field
13. Client Object Model – Getting Started
• No ClientContext = No Connection
clientContext = new ClientContext(“http://mysharepointsite.com”);
• Need to LOAD before you can READ
clientContext.Load(web);
clientContext.Load(web.Lists);
• Must COMMIT requests in a BATCH
clientContext.ExecuteQuery();
clientContext.ExecuteQueryAsync(succeedcallback, failurecallback);
14. 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 through
foreach(ListItem listItem in listItems)
Console.WriteLine(“Title: ,1-”, listItem*“Title”+,
15. 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();
16. 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);
}
17. 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
19. 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”
21. 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)
23. 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
24. 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 wife's 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
25. Thank You
• Personal Email: liamcleary@msn.com
• Work: http://www.susqtech.com
• Twitter: @helloitsliam
• Blog: www.helloitsliam.com