Implementing the SharePoint Foundation REST Interface

SharePoint 2010

Last modified: December 03, 2010

Applies to: SharePoint Foundation 2010

This part of the walkthrough about creating a custom Windows Communication Foundation (WCF) web service shows how to create a Windows Forms Application in Microsoft Visual Studio 2010 that uses ADO.NET Data Services and the Microsoft SharePoint Foundation Representational State Transfer (REST) interface to interact with list data through two DataGridView controls. The SharePoint Foundation REST interface provides the data context for interacting with a Projects list and an Employees list through the two controls. The Employees control displays the employees that are associated with the project item that users have selected in the Projects control. In addition, the Projects control allows users to make changes to items in the Projects data source and to update SharePoint list data with those changes.

  1. Open Visual Studio and click File, point to New, and click Project. In the New Project dialog box, select Visual Basic or Visual C# in the Installed Templates box, and then select Windows, select Windows Forms Application, and type ProjectTracker in the Name box. Click OK.

  2. To connect to data that is stored in a SharePoint site, click Data, and then click Add New Data Source. In the Data Source Configuration Wizard, select SharePoint, and then click Next.

  3. In the Add Service Reference box, type the path to ListData.svc, the service that provides access to the SharePoint Foundation REST interface. This path must include the website that contains the lists and must specify the _vti_bin virtual directory, for example, http:// Server/sites/SiteCollection/WebSite/_vti_bin/ListData.svc. To add the service reference to your project, click Go, click OK, and then click Finish. The service reference connects your application to the REST interface and retrieves an entity data model of the SharePoint Foundation site for defining entity classes. SharePoint Foundation list data is attached to this service reference as a strongly typed data source.

  4. In the Form1.cs Design view, click Data, and then click Show Data Sources to open the Data Sources box. To provide the UI for interacting with list data, drag DataGridView controls for the Projects and Employees lists onto Form1.

  5. To remove unwanted columns from display in each DataGridView control, right-click the control, click Edit Columns, and in the Edit Columns box, select each column in the Selected Columns box and click Remove. From the Projects DataGridView control, remove all columns except Title, Description, and Due Date. From the Employees DataGridView control, remove all columns except Full Name, Job Title, and Team. In the Bound Column Properties box, set the Title column of the Projects list and the Full Name column of the Employees list to Fill, and then click OK.

  6. To populate the DataGridView controls with list data from the SharePoint Foundation website, double-click the edge of Form1 to view its code. Add a using statement to import the ServiceReference1 namespace that you created, and use a constructor to create a strongly typed data context to represent the list data. You can use this data context to send requests to the website. In the constructor, specify the URI of the WCF service that provides the REST interface for accessing list data in the specified website, as follows.

    Imports ProjectTracker.ServiceReference1
    Imports System.Net
    
    Public Class Form1
    
        Private Shared websiteUrl As String = "http://YourServer/sites/YourSiteCollection/YourSite"
    
        Private context As New MyWebsiteDataContext(
            New Uri(websiteUrl + "/_vti_bin/listdata.svc"))
    
        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;
    
    namespace ProjectTracker
    {
        using ServiceReference1;
        using System.Net;    
    
        public partial class Form1 : Form
        {
    
        private static string websiteUrl= "http://YourServer/sites/YourSiteCollection/YourSite";
    
            MyWebsiteDataContext context = new MyWebsiteDataContext(
                new Uri(websiteUrl + "/_vti_bin/listdata.svc"));
    
            public Form1()
            {
                InitializeComponent();
            }
    
  7. In the Form1_Load event, set up authentication for the request. Add a using statement to import the System.Net namespace. Windows integrated authentication allows you to use the default credentials of the credential cache. Also in the Form1_Load event, populate the data source that was generated by Visual Studio for the Projects DataGridView control by using the data context class, as follows.

        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
        End Sub
    

        private void Form1_Load(object sender, EventArgs e)
        {
            context.Credentials = CredentialCache.DefaultCredentials;
            projectsBindingSource.DataSource = context.Projects;
        }
    
  8. To populate the Employees DataGridView control with list data, create a handler for the CurrentChanged event in the Projects data source. When the user selects a project in the Projects DataGridView, the Employees DataGridView will be populated by a filtered list of employees according to the project that is selected. To add the CurrentChanged event in Design View, you can right-click projectsBindingSource beneath the form, click Properties, click the events icon in the Properties window, and double-click the CurrentChanged event.

  9. Use a Language Integrated Query (LINQ) query to filter employees for those whose project identifier (ID) equals the ID of the currently selected project. You can get the currently selected project by using the Current property of project data source and casting the current item as a ProjectsItem type, as follows.

        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 void projectsBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            employeesBindingSource.DataSource =
                from emp in context.Employees
                where emp.Project.Id == ((ProjectsItem)projectsBindingSource.Current).Id
                select emp;
        }
    

    Notice that the example uses the Project navigation property of the Projects list that is mapped from the Project lookup column in the Employees list.

  10. To add update support, so that when users modify data in the form data is modified in the website, enable the Save button in Design view by right-clicking the Save icon in the Form1 menu, and clicking Enabled. Double-click the Save button to view code and insert a projectsBindingNavigatorSaveItem_Click event handler. Add the following line of code to instruct ADO.NET Data Services to save changes back to the website.

        Private Sub ProjectsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingNavigatorSaveItem.Click
            context.SaveChanges()
        End Sub
    

        private void projectsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            context.SaveChanges();
        }
    
  11. Because you are creating a Windows Forms Application, you must add code that instructs the data context about objects that are changed. To add a CurrentItemChanged event, right-click projectBindingSource in Design view, click Properties, click the events icon in the Properties window, and double-click the CurrentItemChanged event to insert the handler in Form1.cs. Use the UpdateObject(Object) method of the ADO.NET data context in this handler, and use the Current property to indicate which property has changed, as follows.

        Private Sub ProjectsBindingSource_CurrentItemChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingSource.CurrentItemChanged
            context.UpdateObject(ProjectsBindingSource.Current)
        End Sub
    End Class
    

        private void projectsBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            context.UpdateObject(projectsBindingSource.Current);
        }
    }}
    
  12. To test the form, press F5 and make some changes to an item in the Projects DataGridView. When you click Save, the changes that you make are propagated to the website.

    Note Note

    You may need to click outside the cell in which you make changes before you click the Save button in order to see the changes take effect.

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

Show: