Sharepoint Client Object Model 1

Sunday, December 1, 2013

Client Object Model

  • API used when building remote applications
    • Three implementations: .NET Managed, Silverlight, JavaScript
    • Alternative to Sharepoint ASMX Web Services
  • Designed to be similar to the Server Object Model
  • Types in COM generally named same as SOM minus 'SP' prefix
  • Methods and properties also named the same when possible
  • Many SOM types or members are not available in COM
    • Example: the COM does not have WebApplication or Farm Type

Server .NET Managed and Silverlight JavaScript
SPContext ClientContext SP.ClientContext
SPSite Site SP.Site
SPWeb Web SP.Web
SPSite.Title Site.Title SP.Site.get_title and SP.Site.set_title()

Communicating with Sharepoint

  • Client Object Model is a facade on top of WCF service
  • Requests for resources batched using Load and LoadQuery methods
  • Requests ares executed using ExecuteQuery or ExecuteQueryAsync
    • XML document with batched request information sent to server
    • JSON response with requested resources returned


  • Windows credentials passed by default in .NET managed OM
  • Can override default
    • ClientContext.AuthenticationMode
      • Anonymous
      • Default
      • Forms Authentication
    • ClientContext.Credentials
      • Explicitly set windows credentials
    • ClientContext.FormsAuthenticationLogInfo

Out of the box teamsite with 4 extra lists

Create a new console application

Add these two references

Get title of site using load and executequery

Output in console window

Look behind the scenes: start up fiddler

Go back to application, execute one more time

Call to the WCF service

Raw view of the request, XML document that gets sent requesting the batch

Raw view of the response, JSON serialized object representing the web

Scroll to the right, see the title, the object of the query

The total length of the json that gets sent back is 797 bytes

Apply filter to only retrieve the title

Execute the app one more time, view the response in fiddler, 276 bytes, a reduction in network traffic by applying the filter

Grab all the lists from site and display the title

Output in console window

Apply filter to web and collection of lists

Retrieving Resources using LoadQuery

  • ClientContext.LoadQuery(clientObjects)
  • Indicates result of query should be included in next batch retrieval
  • Result returned from Call
    • Not loaded in place as with Load

Grab the lists that are not hidden and populated, using loadquery

Get back site title and filtered set of lists

Run the app, fire up fiddler, the json response object return 9698 bytes, the list objects are returning all fields

Modify query to only grab the title

Run the app one more time

Resultset reduced to 1632 bytes

Querying list data

  • Querying lists in SOM is noticeably different than querying lists in COM
    Task ServerOM ClientOM
    Get list web.Lists["Products"] web.Lists.GetByTitle("Products")
    Get items list.Items list.GetItems(query)
    Get item title item.Title item["Title"]
  • Set of items accessed by List.GetItems method
    • API forces use of CAML query to encourage reduced resultset
  • Data-binding items requires workaround
    • ListItemCollection does not have GetDataTable helper found in SOM
    • Need to store column values in temporary data structure to bind

Demo: Create a windows form application

Resize the form, add a couple of labels, a combobox and a listbox

Add references to required assemblies

Conflict both windows and sharepoint have a form class

Resolve using SP and providing the prefix

Or resolve aliasing windows forms

Get the categories from the list

Bound to combobox, combox populated with the categories from the category list

Get the products associated with the category

Resultset in the listbox

Using include to specify what colums get returned from query

View of output in windows form app

Default Success Warning Important Info Inverse
B S W I ? O