Leveraging the SharePoint
ListsWeb Service
This chapter explores the uses of the built-in SharePoint web services, namely the Lists web
service. When creating applications that run outside of the SharePoint environment but need
access to SharePoint lists, the use of the Lists web service is a must. In this chapter, the web
service is explained along with several common operations that you usually need to perform.
WEB SERVICES OVERVIEW
Web services can be considered data sources accessible via http. Using a URL, you may easily
retrieve data by performing web method queries. SharePoint has built-in system web services
that it uses internally but may also be used for custom programming purposes.
SharePoint Web Services Introduction
The SharePoint web services are implemented as ASP.NET Web Services (.asmx). Various
web services provide SharePoint functionality and administrative tasks, which are all physi-
cally located within the ISAPI folder of the “14” hive. You can access them via IIS by using a
virtual directory named
_vti_bin associated to each SharePoint web application, as shown
in Figure4-1. You may view the available web methods for each web service by using the
virtual directory and web service within the browser, as shown in Figure4-2.
Instantiating a Web Service
You may reference the SharePoint web services within your Visual Studio 2010 project and
accordingly instantiate them. This is usually implemented when the application is not running
within SharePoint and therefore does not have access to the SharePoint object model via the
SharePoint assemblies. For example purposes, a console application is used in this chapter;
however, you may follow the same steps for other project/application types.
4
105054c04.indd 73 1/10/12 3:34 PM
74
CHAPTER 4 Leveraging the SharePoint LiStSWeb Service
FIGURE41: The _vti_bin Virtual Directory in IIS
FIGURE42: Viewing the web methods within the browser
105054c04.indd 74 1/10/12 3:34 PM
Web Services Overview
75
The rst task is creating the reference to the SharePoint web service within your Visual Studio project.
To do this, follow these steps:
1. Right-click the project in Visual Studio, and select Add Service Reference, as shown in
Figure4-3. The Add Service Reference dialog appears.
FIGURE43: Add Service Reference menu option
2 Click the Advanced button located in the lower-left corner of the Add Service Reference dialog.
The Service Reference dialog appears.
3. Click the Add Web Reference button located in the lower-left corner of the Service Reference
dialog. The Add Web Reference dialog appears.
4. Enter the address of the web service, and click the Go button, as shown in Figure4-4. The
web methods appear in the dialog window.
5. Enter a name for the web reference, and click the Add Reference button. The web reference is
added to your project and appears within the Solution Explorer window.
Now that the web service is referenced within your project, you need to add code to instantiate
the web service. You accomplish this by creating a new object from the web service object-type.
In the example, the Lists web service is referenced and therefore the object is lists. The object
instantiation syntax is as follows:
ListsWS.Lists listsWS = new ListsWS.Lists();
105054c04.indd 75 1/10/12 3:34 PM
76
CHAPTER 4 Leveraging the SharePoint LiStSWeb Service
FIGURE44: Add Web Reference
You may now use the variable object to call the web methods of the Lists web service, as shown
by the intelli-sense in Figure4-5. However, it is best to set the appropriate credentials so that your
application has access to invoke the methods displayed. This is explained in the next subsection.
Setting Credentials
The web service needs credentials to authenticate against the SharePoint permissions so that the actions
performed by the web methods are correctly done. Otherwise, anyone could invoke the web methods
and gain unauthorized access. You can provide credentials in two ways: by using the default credentials
of the current user or providing specic credentials.
Default Credentials
You may set the credentials of the web service to the current user or account running the application
by leveraging the default network credentials from the credential cache. The syntax using the example
variable is as follows:
listsWS.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
105054c04.indd 76 1/10/12 3:34 PM

Get Professional SharePoint® 2010 Field Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.