How to use the SharePoint Client Object Model

SharePoint 2010 introduced the Client Object Model. In the past there were 2 options to develop an application that uses SharePoint:

  1. Using the object model, but then your application had to run on the SharePoint server
  2. Using the webservices, a little complex

Now there is the Client Object Model. This set of methods takes care of the communication, parsing, …

0ebaeb17-ceb2-43a7-9ebe-22adc04b6137

More on how the Client Object Model works on the Microsoft website.

This post shows you how to work with lists:

1. How do you retrieve data from a list using the client object model?

It takes only few lines of code to retrieve information from a list:

 

            Dim clientContext As New ClientContext(ConfigurationManager.AppSettings("siteUrl"))
            clientContext.Credentials = New Net.NetworkCredential(ConfigurationManager.AppSettings("username"), _
                                                                  ConfigurationManager.AppSettings("password"), _
                                                                  ConfigurationManager.AppSettings("domain"))



            Dim list As List = clientContext.Web.Lists.GetByTitle(ConfigurationManager.AppSettings("ListName"))
            Dim camlQuery As New CamlQuery
            camlQuery.ViewXml = <View/>

            Dim listItems As ListItemCollection = list.GetItems(camlQuery)

            clientContext.Load(listItems)
            clientContext.ExecuteQuery()

2. How do you store data to a list using the client object model?

     

                Dim clientContext As New ClientContext(ConfigurationManager.AppSettings("siteUrl"))
                clientContext.Credentials = New Net.NetworkCredential(ConfigurationManager.AppSettings("username"), _
                                                                      ConfigurationManager.AppSettings("password"), _
                                                                      ConfigurationManager.AppSettings("domain"))

                Dim list As List = clientContext.Web.Lists.GetByTitle(ConfigurationManager.AppSettings("ListName"))

                Dim item As ListItem = list.AddItem(New ListItemCreationInformation())

                item.ParseAndSetFieldValue("Title", "Hello world")

                item.Update()

                clientContext.ExecuteQuery()