Wednesday, July 14, 2010

SharePoint 2010 Client Object Model using ADO.NET and REST Services (Retrieve / Update List items)

As I discussed my previous post the Client Object Model allows client application to interact with SharePoint content directly. The ADO.NET Data Services Framework helps to query SharePoint Foundation data via client application.

In this example you will see .NET Managed client by create Windows Form application, and you will see how to retrieve list items and updating the items via Client Object Model.

1. Open Visual Studio 2010 by going to the Start Menu | Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010.

2. From the menu, select File | New | Project.

3. In the New Project dialog window, choose Visual C# | Windows from the Installed Templates.

4. Select Windows Forms Applications from the Project Templates.

image

5. Go to Visual Studio top menu bar, select Data | Add New Data Source… to add new SharePoint ADO.NET List Data Service.

image

6. In the Data Source Configuration Wizard select Service and click Next > button. When next screen appear Add Service Reference dialog window, type the following Url in the Address text box and click Go:

image

7. Click OK.

8. Click Finish button in the Data Source Configuration Wizard.

9. Go to Solution Explorer, expand References. and Right click on System.Data.Services.Client and select Remove.

 image

10. Right click on References and select Add Reference  and switch to Browse tab and type the following path in the File name textbox and press enter:

C:\Program Files (x86)\ADO.NET Data Services V1.5 CTP2

11. Select Microsoft.Data.Services.Client.dll.

image 

12. Got Visual Studio Data menu and Click Show Data Source System will open the Data Sources window showing the service reference.

image

13. Drag and drop Tasks from the Data Sources window to the Form window. This will insert the Tasks DataGrid into the Form.

14. You can Remove columns by clicking Edit Columns window, Keep only following columns as shown in the screen shot.

image

image

15. Add the following using statement in the code behind:

C#, using GeSHi 1.0.8.8
  1. using SharePointADORESTService.ServiceReference1;
  2. using System.Net;

16. Insert the following code just after the Form1 class declaration:

C#, using GeSHi 1.0.8.8
  1. HomeDataContext context = new HomeDataContext(new Uri("http://demo2010a:1000/_vti_bin/ListData.svc"));

17. Insert the following code in the Form1_Load method

C#, using GeSHi 1.0.8.8
  1. context.Credentials = CredentialCache.DefaultCredentials;
  2. tasksBindingSource.DataSource = context.Tasks;

18. To Update changes from Data Grid to go Tasks DataGrid, select the BindingNavigator right click on the Save button and Select Enable.

image

19. Double click on the Save button in the BindingNavigator to generate the Save method:

C#, using GeSHi 1.0.8.8
  1. context.SaveChanges();

20. Go to TaskBindingSource –> Right Click –> and Go to Properties Select Events. Double click on CurrentItemChanged event. Visual Studio will generate the item changed event for the TasksBindingSource:

C#, using GeSHi 1.0.8.8
  1. context.UpdateObject(tasksBindingSource.Current);

21. Press F5 to start debugging the application.

22. You should see the application window with the list of Tasks items from SharePoint List.

image

23. Click on the first tasks and change the Title value from Faizal from Rafi and click Save and go to SharePoint Tasks list you will able to view the updates. 

image

For Source Code click here

No comments: