Using Data From SharePoint 2003 Lists (Revisited)

Wow, It's been way too long since I blogged.  I've been pretty busy lately with PDC and the MVP Summit, both of which were great events.  So as an excuse to post something hopefully useful, I'll take this opportunity to answer a question from a reader about my earlier post on using data from SharePoint lists.

 

Q:  Joe Chan writes "How do you create a web reference to a subsite containing a space in the URL?"

A:  Let's start by looking at what is going on behind the covers when you add a web reference.  When you use the familiar dialog box to add a web reference to a .NET based web service, Visual Studio shows you the web service helper page and at the same time requests the WSDL for the web service by calling the web service url with a "?wsdl" query string.  The URL is similar to this: http://myserver/_vti_bin/Lists.asmx?wsdl.  The ASP.NET web service infrastructure knows that this query string means to return the WSDL for the service which is then generated (using Reflection) and returned to the browser as XML.  The Add Web Reference dialog then uses that WSDL to generate a proxy class for the web service.

For reasons I'm not entirely certain of, SharePoint can't return the WSDL for a web service with a space in the URL.  It works fine for a regular ASP.NET web service, but if you browse to http://myserver/Sub%20Site/Lists.asmx?wsdl you'll see a "File Not Found" error.  Now while I'm sure that some other true SharePoint diety could probably tell me why this is, it's such a minor inconvenience that it's not worth investigating. 

Here's the workaround.  Create a web reference to your primary SharePoint site (I'm assuming here that there isn't a space in it's URL).  Then click on the  "Show All Files" button in the Solution Explorer.  Expand the web reference all the way and your screen should look something like the one below. 

 

Notice the Reference.vb file.  This file contains the .NET proxy code generated using the WSDL from your site's web service  Because SharePoint uses the same services no matter which site you are requesting, you can simply change the hardcoded (who's idea was that anyway) URL in the constructor to be the URL for your subsite and it will work like a charm.  Here's some sample code for that.

 

1 Comment

Comments have been disabled for this content.