Exercise 3: Using ADO.NET Data Services
In this exercise you will create an application using ADO.NET Data Services. This exercise requires the installation of ADO.NET Data Services version 1.5 (CTP2). You should also have completed Lab 5 LINQ to SharePoint because you will reuse the lists from that lab in this exercise.
- In Visual Studio 2010, create a new project by selecting File » New » Project.
- In the New Project dialog, expand the nodes Visual C#/Visual Basic » Windows and select Windows Form Application. Pay attention that the Framework 3.5 is selected.
- Name the new project ProjectManagers and click the OK button.
- When the new project opens, right-click the References node in the Solution Explorer and select Add Service Reference.
- In the Add Service Reference dialog, enter the URL for the ListData Web Service. This service is available through the ListData.svc endpoint. You can create a URL for the service by appending _vti_bin/ListData.svc to the URL for the site you created in a previous lab (e.g., https://intranet.contoso.com/sites/Lab05/_vti_bin/ListData.svc).
Once you have located the ListData service, change the namespace in the Add Service Reference to MyDataService. Then click the OK button.
When Visual Studio makes the service reference, it uses the functionality found in System.Data.Service.Client. However, you need to replace this reference with a reference to Microsoft.Data.Services.Client, which is part of the ADO.NET Data Services 1.5 CTP2.
Figure 3
The System.Data.Services.Client reference
- Right click the System.Data.Services.Client reference in the Solution Explorer and select Remove from the menu.
- Right click the References node in the Solution Explorer and select Add Reference.
In the Add Reference dialog, click the Browse tab. Navigate to C:\Program Files (x86)\ADO.NET Data Services V1.5 CTP2\bin. Add a reference to Microsoft.Data.Services.Client.dll.
Figure 4
Add a reference to the Microsoft.Data.Services.Client
In addition to replacing the client-side assembly, you must also make new proxy classes for your project. This is because Visual Studio does not use the proxy creation capability found in ADO.NET Data Services 1.5 CTP2. You can make the new proxies using a command-line utility.
- Open a command window and change the directory to C:\Program Files (x86)\ADO.NET Data Services V1.5 CTP2\bin.
- At the command prompt, type the following to generate new proxy classes:
DataSvcUtil.exe /uri:https://intranet.contoso.com/sites/Lab05/_vti_bin/ListData.svc /language:CSharp /out:Reference.cs
DataSvcUtil.exe /uri:https://intranet.contoso.com/sites/Lab05/_vti_bin/ListData.svc /language:VB /out:Reference.vb
- Copy the new file Reference.cs (found in the same directory where the DataSvcUtil.exe was executed) over existing file with the same name in the MyDataService folder of your project. Your project is now all set to use the new ADO.NET Data Services.
In Visual Studio open the Data Sources window by selecting Data » Show Data Sources from the menu. In this window, you should see the lists available on the SharePoint site.
Figure 5
The Data Sources window
If you don’t see data sources listed in the Data Sources window, right-click the MyDataService reference in the Solution Explorer and select Update Service Reference. If you do this, make sure you go back and remove the reference to System.Data.Services.Client that was re-added by Visual Studio.
- In the Data Sources window, click the Add New Data Source button (left-most button).
- In the Data Source Configuration Wizard, select Object and click the Next button.
On the Select Data Objects screen, expand the ProjectManagers treeview. Select EmployeesItem and ProjectsItem and click the Finish button.
Figure 6
The Data Source Configuration Wizard
Drag the ProjectsItem data source from the Data Sources window and drop it on the Windows form. This action will create a grid on the form.
Figure 7
Drag and drop the ProjectItems data source
- Right click the grid and select Edit Columns from the context menu. Remove all of the columns except for Title, and Description. Then click the OK button.
- To work with LINQ in SharePoint, you need to add a reference to a new assembly. Do this with the following steps:
- Select Project » Add Reference from the Visual Studio main menu.
- In the Add Reference dialog, on the .NET tab select Microsoft.SharePoint.Linq and click the OK button.(Note: if you cannot find it here, Browse to c:\Program Files\Common Files\Microsoft Shared\web server extensions\14\ISAPI and select Microsoft.SharePoint.Linq.dll. Then click the OK button.
- Open the code window for the form and add the following statements to the top of the code module.
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.Linq; using System.Net;
Imports Microsoft.SharePoint.Linq Imports System.Net
- Add code to create a context object so that your project matches the following.
namespace ProjectManagers { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //Context MyDataService.Lab5UsingLINQToSharePointDataContext ctx = new MyDataService.Lab5UsingLINQToSharePointDataContext(new Uri( "https://intranet.contoso.com/sites/Lab05/_vti_bin/ListData.svc")); }
Public Class Form1 'Context Private ctx As New MyDataService.Lab5UsingLINQToSharePointDataContext(New Uri("https://intranet.contoso.com/sites/Lab05/_vti_bin/ListData.svc")) End Class
If you encounter an error after having typed in MyDataService, remove the complete statement and type Lab5UsingLINQToSharePointDataContext. Right-click this and choose Resolve. Click on the suggested using statement.
- In the Form1_Load event, add the following code to bind the list items to the grid. If the Form1_Load event doesn’t exist, go back to the Design and double-click the form to create it.
private void Form1_Load(object sender, EventArgs e) { ctx.Credentials = CredentialCache.DefaultCredentials; var q = from p in ctx.Projects select p; this.projectsItemBindingSource.DataSource = q; }
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ctx.Credentials = CredentialCache.DefaultCredentials Dim q = From p In ctx.Projects _ Select p Me.projectsItemBindingSource.DataSource = q End Sub
- Run the project now and you should see some results in the grid.
Return to Visual Studio. Open Form1 in Design view and right-click the save icon on the form and select Enabled from the context menu.
Figure 8
Configure the data source
- Now add the following code to your project to enable saving the changes you make in the grid.
private void projectsItemBindingNavigatorSaveItem_Click(object sender, EventArgs e) { ctx.SaveChanges(); } private void projectsItemBindingSource_CurrentChanged(object sender, EventArgs e) { ctx.UpdateObject(this.projectsItemBindingSource.Current); }
Private Sub projectsItemBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) ctx.SaveChanges() End Sub Private Sub projectsItemBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) ctx.UpdateObject(Me.projectsItemBindingSource.Current) End Sub
Go back to the Design view, right click the Save button and select Properties. This will open the Properties tool window. In the Click event, select projectsItemBindingNavigatorSaveItem_Click.
Figure 9
Bind the click event to the SaveItem event handler
In the drop down at the top of the Properties tool window, change the selection from projectsItemBindingNavigatorSaveItem to projectsItemBindingSource. In the CurrentChanged event, select projectsItemBindingSource_CurrentChanged.
Figure 10
Bind the CurrentChanged event to the event handler
- Run the project again and make a few changes to the existing items clicking the Save button when finished (make sure you change the focus so the control knows there are changes to a specific item). Now open the browser and go find the item you just changed to see if in fact your changes were persisted.
In this exercise you utilized ADO.NET Services to retrieve and update items within a SharePoint list.
|
|