Share via


Implementing the Client-Side Object Model

Applies to: SharePoint Foundation 2010

The previous part in this walkthrough, Implementing the SharePoint Foundation REST Interface, shows how to use the ADO.NET Data Services and the SharePoint Foundation REST interface to work with data that is stored in lists and libraries. However, the REST interface does not provide access to other data in SharePoint Foundation sites, as may be required in more advanced applications. In previous versions of SharePoint Foundation, you might have used the default web services to access data, and then defined wrapper classes and converted the returned SOAP XML into objects for your application. Microsoft SharePoint Foundation 2010 introduces a managed client-side object model that provides access to a broader set of SharePoint Foundation objects than the new REST interface, at website level or lower within the hierarchy of objects, and which makes it easier to work with data in a remote application than it is to use the web services.

This example assumes that you created the Windows Forms Application that is described in Implementing the SharePoint Foundation REST Interface. The example uses the client object model to set the text in the form's title bar to the website's title, and to change the description of the Projects list on the site based on the project item that is selected when the user clicks a Star Project button in the form.

Using the Client Object Model to Change the Form Title and List Description

  1. Add a reference to two client object model assemblies, Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll. Right-click References in Solution Explorer, and then click Add Reference. On the .NET tab of the Add Reference dialog box, select Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime, and then click OK.

  2. In Form1.cs or Form1.vb, add statements to import the Microsoft.SharePoint.Client namespace into the project. Use the client context to gain entry to the client object model, using a ClientContext() constructor to connect to the website, as follows.

    Imports ProjectTracker.ServiceReference1
    Imports System.Net
    Imports Microsoft.SharePoint.Client
    
    Public Class Form1
    
        Private Shared websiteUrl As String = "http://YourServer/sites/YourSiteCollection/YourWebSite"
    
        Private context As New TestWebsDataContext(
            New Uri(websiteUrl  + "/_vti_bin/listdata.svc"))
    
        Private clientContext As New ClientContext(websiteUrl)
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.SharePoint.Client;
    
    namespace ProjectTracker
    {
        using ServiceReference1;
        using System.Net;
    
        public partial class Form1 : Form
        {
    
            private static string websiteUrl= "http://YourServer/sites/YourSiteCollection/YourWebSite";
    
            TestWebsDataContext context = new TestWebsDataContext(
                new Uri(websiteUrl + "/_vti_bin/listdata.svc"));
    
            ClientContext clientContext = new ClientContext(websiteUrl);
    
            public Form1()
            {
                InitializeComponent();
            }
    
  3. Use the Form1_Load event to send the request to the SharePoint Foundation server. The client object model automatically uses the default credentials. To use an object, you must call the Load<T>(T, []) method to explicitly request loading the object’s properties, and then call the ExecuteQuery() method (or in the Microsoft Silverlight version of the object model, the ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) method), to send the request to SharePoint Foundation and populate the website object with data. In the following snippet, the title bar of the form is set to the title of the website.

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            context.Credentials = CredentialCache.DefaultCredentials
    
            ProjectsBindingSource.DataSource = context.Projects
    
            clientContext.Load(clientContext.Web)
            clientContext.ExecuteQuery()
            Me.Text = clientContext.Web.Title
        End Sub
    
        Private Sub ProjectsBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingSource.CurrentChanged
            EmployeesBindingSource.DataSource =
                From emp In context.Employees _
                Where emp.Project.Id = DirectCast(ProjectsBindingSource.Current, ProjectsItem).Id _
                Select emp
        End Sub
    
        Private Sub ProjectsBindingSource_CurrentItemChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingSource.CurrentItemChanged
            context.UpdateObject(ProjectsBindingSource.Current)
        End Sub
    
        Private Sub ProjectsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingNavigatorSaveItem.Click
            context.SaveChanges()
        End Sub
    
            private void Form1_Load(object sender, EventArgs e)
            {
                context.Credentials = CredentialCache.DefaultCredentials;
                projectsBindingSource.DataSource = context.Projects;
    
                clientContext.Load(clientContext.Web);
                clientContext.ExecuteQuery();
    
                this.Text = clientContext.Web.Title;
    
            }
    
            private void projectsBindingSource_CurrentChanged(object sender, EventArgs e)
            {
                employeesBindingSource.DataSource =
                    from emp in context.Employees
                    where emp.Project.Id == ((ProjectsItem)projectsBindingSource.Current).Id
                    select emp;
            }
    
            private void projectsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
            {
                context.SaveChanges();
            }
    
            private void projectsBindingSource_CurrentItemChanged(object sender, EventArgs e)
            {
                context.UpdateObject(projectsBindingSource.Current);
            }
    
  4. In addition to changing the title of the form, this example also changes the description of the Projects list that is displayed in the website based on user selection. When the user selects an item in the Projects list and clicks a button, the description of the list changes to highlight the selected project. To add a button to Form1, right-click the form title bar next to the Save button, and then select Button in the drop-down list that appears.

  5. In the Properties window for the button, set DisplayStyle to Text, and type Star Project as the value for the Text setting.

  6. Double-click the button in the form to open its Click event, and add the following code, which uses the GetByTitle(String) method of the client object model to return the list and access its description. The example uses the ADO.NET Data Services Projects data source to get the title of the currently selected project, and casts the selected item as a ProjectsItem object to access the Title property of the item.

        Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
            Dim oList As List = clientContext.Web.Lists.GetByTitle("Projects")
    
            oList.Description = String.Format("Star Project of the Week is {0}!!!", DirectCast(ProjectsBindingSource.Current, ProjectsItem).Title)
    
            oList.Update()
            clientContext.ExecuteQuery()
        End Sub
    End Class
    
            private void toolStripButton1_Click(object sender, EventArgs e)
            {
                List oList = clientContext.Web.Lists.GetByTitle("Projects");
    
                oList.Description = string.Format("Star Project of the Week is {0}!!!",
                    ((ProjectsItem)projectsBindingSource.Current).Title);
    
                oList.Update();
                clientContext.ExecuteQuery();
            }
    }}
    
  7. Press F5 to run the application to see that the form’s title has changed. Select one of the items in the Projects DataGridView control, and then click Star Project to change the list’s description.

For the complete Form1 code sample, see Complete SharePoint Foundation WCF Form1 Sample.

See Also

Concepts

Walkthrough: Creating and Implementing a Custom WCF Service in SharePoint Foundation

How to: Retrieve Lists

How to: Create, Update, and Delete Lists

Other Resources

SharePoint 2010 Client Object Model