Integrate SharePoint List Data in Your WPF Application

With the SharePoint 2010 Beta came out of the door, one of the exciting news is that it allows developers to access SharePoint List Data using ADO.Net Data Service 1.5. If you are familiar with Data Service Client programming mode (we have already posted some articles here), it will be very easy to integrate SharePoint List Data in your application.

Once you set up the SharePoint with ADO.Net Data Service (see How-To here ), you can check the Data Service through https://<SharePointServer>/_vti_bin/ListData.svc. Now we are going to show you how to program against the service in Visual Studio 2010 Beta2.

In Visual Studio 2010, developers can add Data Source to ADO.Net Data Service through Data Source Configuration Wizard, you can do the same thing for SharePoint Data service. Here we create a Visual Basic WPF Application, after launching the Wizard (Menu->Data->Add New Data Source), we can see a SharePoint entry as following.

clip_image002

After selecting the SharePoint Icon and clicking “Next” button, “Add Service Reference” will be launched. Instead of remembering the long address of the service, you only need to input the SharePoint site (eg, my sample site is https://vspro-vm-bdc1 ) then click “Go” button. Visual Studio will heuristically find the service address for you. Once it’s populated, you can click “OK” button and finish adding the data source.

clip_image004

clip_image006

Then you can open the Data Source Window (Menu->Data->Show Data Source) and check the schema of the SharePoint List Data.

clip_image007

As same as other data sources we introduce before, we can drag the SharePoint Data Source from Data Source Window and drop to WPF Designer. It will generate the UI with Data Binding automatically.

clip_image009

If you open the code-behind file (MainWindow.xaml.vb), you will notice Visual Studio has already generated the sample code so that you can simply follow the comment to fill the data.

Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

Dim AnnouncementsViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("AnnouncementsViewSource"), System.Windows.Data.CollectionViewSource)

'Load data by setting the CollectionViewSource.Source property:

'AnnouncementsViewSource.Source = [generic data source]

End Sub

As we mentioned before, accessing SharePoint List Data is the same as other ADO.Net Data Service. The following is a sample code to load data using the default credential.

Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

Dim AnnouncementsViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("AnnouncementsViewSource"), System.Windows.Data.CollectionViewSource)

'Load data by setting the CollectionViewSource.Source property:

Dim Context As New SharePointReference.TeamSiteDataContext(

New Uri("https://vspro-vm-bdc1/_vti_bin/ListData.svc", UriKind.Absolute))

Context.Credentials = System.Net.CredentialCache.DefaultCredentials

AnnouncementsViewSource.Source = Context.Announcements.Execute().ToList()

End Sub

So far we have finished the simple application with the SharePoint List Data Source. Cheers!

Comments

  • Anonymous
    November 30, 2009
    Hi, probably the How-To link at the top of the article is pointing to an uncorrect resource. Thanks for the great post!

  • Anonymous
    November 30, 2009
    Thanks for the finding Alessandro! And sorry for the mistake. The link has been updated to the correct one. Thanks, Xiaoying Program Manager