Share via


Exercise 1: Create a Silverlight Application that Calls the Lists Web Service

Task 1 – Create a Silverlight application which calls ASMX web services

In this task, you will create a Silverlight application which creates list items in an existing list using the SharePoint Lists web service.

  1. Launch Visual Studio 2010 and open the lab project by selecting File >> Open >> Project.
  2. Browse to the AddTimeEntryWS.sln file located at C:\%Office365TrainingKit%\Labs\4.3\Source\Before and open it.
  3. Expand the AddTimeEntryWS project in the Solution Explorer open the MainPage.xaml file.
  4. Replace all the markup in MainPage.xaml with the following:

    (Toolbox code snippet 4.3.1)

    XAML

    <UserControl x:Class="AddTimeEntryWS.MainPage" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="https://schemas.microsoft.com/expression/blend/2008" xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" xmlns:sdk="https://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White" > <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <sdk:Label Grid.Column="0" Grid.Row="2" Margin="5" Content="Task:"/> <ComboBox Name="ddlTask" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2" Margin="5" > <ComboBox.Items > <ComboBoxItem Content="Create Intranet Portal"></ComboBoxItem> <ComboBoxItem Content="Manage Internet"></ComboBoxItem> <ComboBoxItem Content="SQL Server 2008 R2 Upgrade"></ComboBoxItem> <ComboBoxItem Content="Create Intranet Portal"></ComboBoxItem> <ComboBoxItem Content="Azure Training"></ComboBoxItem> <ComboBoxItem Content="Office 365 Training"></ComboBoxItem> </ComboBox.Items> </ComboBox> <sdk:Label Grid.Column="0" Grid.Row="3" Margin="5" Content="Employee:" /> <ComboBox Name="ddlEmployee" Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="2" Margin="5" > <ComboBox.Items > <ComboBoxItem Content="Andrea Dunker"></ComboBoxItem> <ComboBoxItem Content="Lars Hansson"></ComboBoxItem> <ComboBoxItem Content="Lukas Keller"></ComboBoxItem> <ComboBoxItem Content="Michael Lund"></ComboBoxItem> <ComboBoxItem Content="Neil Orint"></ComboBoxItem> <ComboBoxItem Content="Sanjay Patel"></ComboBoxItem> <ComboBoxItem Content="Scott Bishop"></ComboBoxItem> </ComboBox.Items> </ComboBox> <sdk:Label Grid.Column="0" Grid.Row="4" Margin="5" Content="Date:" /> <sdk:DatePicker Name="dtWorkDate" Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="2" Margin="5" /> <sdk:Label Grid.Column="0" Grid.Row="5" Margin="5" Content="Hours:" /> <TextBox Name="txtHours" Grid.Column="1" Grid.Row="5" Grid.ColumnSpan="2" Margin="5" /> <sdk:Label Grid.Column="0" Grid.Row="6" Margin="5" Content="Description:" /> <TextBox Name="txtDescription" Grid.Column="1" Grid.Row="6" Grid.ColumnSpan="2" Margin="5" /> <Button Name="btnSave" Grid.Column="1" Grid.Row="7" Margin="5" HorizontalAlignment="Stretch" Content="Save" Click="btnSave_Click" /> <Button Name="btnClear" Grid.Column="2" Grid.Row="7" Margin="5" HorizontalAlignment="Stretch" Content="Clear" Click="btnClear_Click" /> </Grid> </UserControl>
  5. Save MainPage.xaml.
  6. When you open MainPage in Design view, it should look like the screenshot below:

  7. Right-click the AddTimeEntryWS project and select Add Service Reference to add a service reference to the Lists.asmx service.
  8. In Address, enter the Url of the Lists service in your current site, e.g. https://intranet.contoso.com/lab04/_vti_bin/lists.asmx, and click Go to retrieve the service information.
  9. In the Namespace textbox enter SPListsWebService.

  10. Click OK.

  11. In the AddTimeEntryWS project, right-click on MainPage.xaml and select View Code and open its code behind.
  12. Add the GetListServiceProxy method to the MainPage.xaml.cs file to return a proxy to the Lists web service (TODO 4.3.2).

    C#

    private ListsSoapClient GetListsServiceProxy() { var proxy = new ListsSoapClient(); proxy.Endpoint.Address = new EndpointAddress (ClientContext.Current.Url + _listASMXUrl); return proxy; }
  13. Right-click on the ListsSoapClient return type in the GetListsServiceProxy method and choose Resolve to include the appropriate using statement at the top of MainPage.xaml.cs.

  14. Locate the Save method in MainPage.xaml.cs and add the following code to its body (TODO 4.3.3).

    The Save method will retrieve a web service proxy, set up the asynchronous callback method and create the Xml with the batch update information. The method then calls UpdateListItemsAsync passing in the list name and batch information as parameters.

    C#

    ListsSoapClient proxy = GetListsServiceProxy(); proxy.UpdateListItemsCompleted += new EventHandler<UpdateListItemsCompletedEventArgs>(proxy_UpdateListItemsCompleted); XElement batch = new XElement("Batch", new XAttribute("OnError", "Continue"), new XAttribute("ListVersion", 1), new XElement("Method", new XAttribute("ID", 1), new XAttribute("Cmd", "New"), new XElement("Field", new XAttribute("Name", "Hours"), txtHours.Text), new XElement("Field", new XAttribute("Name", "BillableDate"), dtWorkDate.SelectedDate.Value.ToString("yyyy-MM-dd")), new XElement("Field", new XAttribute("Name", "Task"), ddlTask.SelectionBoxItem), new XElement("Field", new XAttribute("Name", "Employee"), ddlEmployee.SelectionBoxItem), new XElement("Field", new XAttribute("Name", "BillableDescription"), txtDescription.Text) )); proxy.UpdateListItemsAsync("Billable Time", batch);
  15. Locate the proxy_UpdateListItemsCompleted callback method and the following code to its body (TODO 4.3.4):

    This method is called when the UpdateListItems method returns. This method will do a simple check for the results and display an appropriate message. A more complicated batch with multiple Method nodes would require a more complicated results check. Notice the method uses the Dispatcher object to invoke the method on the UI thread.

    C#

    if (e.Result.Value == "0x00000000") { Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show("Billable time updated successfully !"); ClearForm(); }); } else { Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show("Error adding new billable time!"); }); }
  16. Right-click the AddTimeEntryWS project in the Solution Explorer window and select Build.