Wednesday, June 30, 2010

Programming Against a List Using the Lists Web Service


Program against lists in SharePoint 2010, which enables you to manage, create, read,

update, and delete (CRUD) operations against the list. To program against a list, follow these steps:

1. Open Visual Studio 2010 and click File ➪ New ➪ Project. In the New Project dialog, navigate to
the Windows templates under the Installed Templates gallery and select “WPF application.”

2. Provide a name for your application (forexample, WPFSPListApp) and a location,
and then click OK. Visual Studio 2010will create a new solution for you that
includes a number of files. Right-click the MainWindow.xaml file, and select View
Designer (if the view is not already open).

3. Add five labels, four textboxes, and three buttons to your Designer from the Toolbox
so that the UI looks similar to Figure 2-20. Table 2-1 provides a summary of the control
types and names that you will add to the WPF application.

4. The UI uses a type of XML syntax called Extended Application Markup Language (XAML), which
is specific to Windows WPF and Silverlight applications. The XML must be well formed, and,
when you drag and drop controls from the Toolbox onto the designer surface, the XAML will
automatically be generated for you. You’ll need to add a couple of event handlers to the button
controls to manage the loading of the SharePoint list data into the application. So, after you add
the button controls to the Designer, go to the XAML code view, click your mouse within the button
element, and press the spacebar. This will trigger IntelliSense, allowing you to select the Click
event. Accept the default event handler name, and Visual Studio will add a method for your buttons
in the code behind. The XAML for your application should look something like the following:

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”MainWindow” Height=”300” Width=”500”>
Height=”28”
HorizontalAlignment=”Left”
Margin=”21,14,0,0”
Name=”lblTitle”
VerticalAlignment=”Top”
Width=”162”
FontWeight=”Bold”
FontSize=”13” />
Height=”28”
HorizontalAlignment=”Left”
Margin=”21,56,0,0”
Name=”lblCompanyName”
VerticalAlignment=”Top”
Width=”120” />
Height=”28”
HorizontalAlignment=”Left”
Margin=”21,90,0,0”
Name=”lblRegion”
VerticalAlignment=”Top”
Width=”120” />
Height=”28”
HorizontalAlignment=”Left”
Margin=”21,124,0,0”
Name=”lblSize”
VerticalAlignment=”Top”
54 ❘ Chapter 2 Getting Started with SharePoint 2010 Development
Width=”120” />
Height=”28”
HorizontalAlignment=”Left”
Margin=”21,158,0,0”
Name=”lblSales”
VerticalAlignment=”Top”
Width=”120” />
HorizontalAlignment=”Left”
Margin=”119,56,0,0”
Name=”txtbxCompanyName”
VerticalAlignment=”Top”
Width=”245” />
HorizontalAlignment=”Left”
Margin=”119,90,0,0”
Name=”txtbxRegion”
VerticalAlignment=”Top”
Width=”245” />
HorizontalAlignment=”Left”
Margin=”119,124,0,0”
Name=”txtbxSize”
VerticalAlignment=”Top”
Width=”245” />
HorizontalAlignment=”Left”
Margin=”119,158,0,0”
Name=”txtbxSales”
VerticalAlignment=”Top”
Width=”245” />
Height=”23”
HorizontalAlignment=”Left”
Margin=”29,218,0,0”
Name=”btnUpdate”
VerticalAlignment=”Top”
Width=”75”
Click=”btnUpdate_Click” />
Height=”23”
HorizontalAlignment=”Left”
Margin=”119,218,0,0”
Name=”btnClear”
VerticalAlignment=”Top”
Width=”75”
Click=”btnClear_Click” />
Height=”23”
HorizontalAlignment=”Left”
Margin=”210,218,0,0”
Name=”btnExit”
VerticalAlignment=”Top”
Width=”75”
Click=”btnExit_Click” />
Working with SharePoint Lists ❘ 55

5. Right-click the MainWindow.xaml file, and then click View Code. This will open up the code view.

6. Right-click the References project node, and select Add Service Reference. On the Add Service
Reference dialog, click the Advanced button and then click Add Web Reference on the Service
Reference Settings dialog.

7. In the Add Web Reference dialog, click the Web services on the local machine link. This will search for and display all of the Web services that are located on your developer machine, which will include the SharePoint Web services,

8. One of the Web services is the Lists service (with the endpoint listed as http:///_
vti_bin/Lists.asmx). Select this service. Note that you may need to change the Web service
URL to reflect your local server, for example http://fabrikamhockey/_vti_bin/Lists.asmx.
Provide a name for the service (for example, MySPWebService) and click Add Reference. (You can
also explore the Web methods that are a part of that service before you click Add Reference.)
9. At this point, you can add an event handler for each of the buttons in your WPF UI (which should
already be stubbed out for you). The Update button is the one button that will leverage the
Web service connection to SharePoint. You’ll also require a set of class-level variables to get the
user input and pass that into the Lists Web service. When you call the Lists Web service, you’ll
also need to create an XML construct that passes the data from your WPF application to your
SharePoint list. This XML is called the Collaborative Application Markup Language (CAML).
56 ❘ Chapter 2 Getting Started with SharePoint 2010 Development
10. The following code snippet illustrates the three event handlers, one for each of the buttons. The
bolded code is what you will need to add to the default code that is created for you by Visual
Studio. If you use the accompanying source code, you’ll need to ensure that you update the Web
service reference (by re-adding the service to the Visual Studio project), and update any URL references
in the code. For example, you would need to update the following line of code:
myListService.Url =
http:///_vti_bin/Lists.asmx”;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
namespace WPFSPListApp
{
public partial class MainWindow : Window
{
string strCompanyName = ““;
string strRegion = ““;
string strSize = ““;
string strSales = ““;
string strListID = ““;
string strViewID = ““;
public MainWindow()
{
InitializeComponent();
}
private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
strCompanyName = txtbxCompanyName.Text;
strRegion = txtbxRegion.Text;
strSize = txtbxSize.Text;
strSales = “$” + txtbxSales.Text;
WPFSPListApp.MySPWebService.Lists myListService =
new MySPWebService.Lists();
myListService.Credentials =
System.Net.CredentialCache.DefaultCredentials;
myListService.Url =
http://fabrikamhockey/_vti_bin/Lists.asmx”;
XmlNode myListView = myListService.GetListAndView(“Customers”, ““);
Working with SharePoint Lists ❘ 57
strListID = myListView.ChildNodes[0].Attributes[“Name”].Value;
strViewID = myListView.ChildNodes[1].Attributes[“Name”].Value;
XmlDocument myListDoc = new XmlDocument();
XmlElement batchXML = myListDoc.CreateElement(“Batch”);
batchXML.InnerXml = “” +
strCompanyName + “” + strRegion +
” + strSize +
” + strSales +
” + “”;
XmlNode myListReturn = myListService.
UpdateListItems(strListID, batchXML);
MessageBox.Show(“SharePoint List was updated!”);
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
txtbxCompanyName.Text = ““;
txtbxRegion.Text = ““;
txtbxSales.Text = ““;
txtbxSize.Text = ““;
}
private void btnExit_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
}
}
11. Assuming that your code reflects what is shown here, you should now be able to press F5 and run the application in Debug mode, add some string entries to the WPF application, and click Update to add the record to your SharePoint list, as shown in Figure 2-22.
Figure 2-22 Updated list How It Works The SharePoint Web services offer quite a range of functionality for the developer and should be one of your first stops when developing for SharePoint (to leverage what already ships with SharePoint).Many of these are services that were available in SharePoint 2007 and have evolved to be supported in SharePoint 2010. For those who used them in production code in 2007, this is good news, because upgrading your 2007 code should not prove too difficult.For this example, you used the Lists Web service, which provides a number of different ways to interact with a list — for example, you can add or delete a list, add an attachment to a list, get the list, and soon. In this example, you used the GetListAndView Web method, which returns a schema for a list that you pass in as a parameter to the GetListAndView method call. Note that, in this call, you passed the
name of the list, Customers, and mapped the return value to an XMLNode object.

XmlNode myListView = myListService.GetListAndView(“Customers”, ““);

The example also used CAML to insert data back into the SharePoint list. Admittedly, CAML is a little overbose, as you can see from the following line of code. (You’ll see different ways to interact with a list.

batchXML.InnerXml = “” +
strCompanyName + “” + strRegion +
” + strSize + “
+ strSales + “” + “”;
The last key piece in this example was the UpdateListItems method, which passed the list ID (that is,
the name of the list) and the list schema that was mapped to the CAML construct (which was further
tied to the data in the WPF client).
XmlNode myListReturn = myListService.UpdateListItems(strListID, batchXML);
While this method leverages native Web services, there are both pros and cons to using them. Pros

include ease of use and service plumbing that exists, as opposed to your having to create a custom Webservice. Cons include potential performance hits with service integration and syntax verbosity with theCAML construct.

If you followed along with this example and successfully updated your SharePoint list, then
congratulations!
You just wrote your first application against SharePoint 2010 that interacts with a
SharePoint list.