Preface.
In the part one post on this topic, I explained why I thought the SharePoint Object Model was not letting me develop on one machine and access SharePoint information on another server. So I'll not spend any time on that here. Instead, I'll just show what I did to get the list of sub-sites from my root SharePoint site.
Quick Overview.
To work with the SharePoint web services, you need to know that there are actually 16 SharePoint web services. It's older technology and a bit more cumbersome.but that's for another post. Here are the 16 services:
- http://<server>:<port>/_vti_adm/Admin.asmx – Administrative methods such as creating and deleting sites
- http://<server>/_vti_bin/Alerts.asmx – Methods for working with alerts
- http://<server>/_vti_bin/DspSts.asmx – Methods for retrieving schemas and data
- http://<server>/_vti_bin/DWS.asmx – Methods for working with Document Workspaces
- http://<server>/_vti_bin/Forms.asmx – Methods for working with user interface forms
- http://<server>/_vti_bin/Imaging.asmx – Methods for working with picture libraries
- http://<server>/_vti_bin/Lists.asmx – Methods for working with lists
- http://<server>/_vti_bin/Meetings.asmx – Methods for working with Meeting Workspaces
- http://<server>/_vti_bin/Permissions.asmx – Methods for working with SharePoint Services security
- http://<server>/_vti_bin/SiteData.asmx – Methods used by Windows SharePoint Portal Server
- http://<server>/_vti_bin/Sites.asmx – Contains a single method to retrieve site templates
- http://<server>/_vti_bin/UserGroup.asmx – Methods for working with users and groups
- http://<server>/_vti_bin/versions.asmx – Methods for working with file versions
- http://<server>/_vti_bin/Views.asmx – Methods for working with views of lists
- http://<server>/_vti_bin/WebPartPages.asmx – Methods for working with Web Parts
- http://<server>/_vti_bin/Webs.asmx – Methods for working with sites and sub-sites
(Replace <server> with the name of the hosting server and <port> with the port used by Central Administration. )
There is a lot more information about the web methods available in the SharePoint Products and Technologies 2003 SDK.
Nuts and Bolts.
Next, I'll build a simple client app using Visual Basic .Net 2005. Sorry, but all the Visual C# fans will just have to make the necessary code adjustments.
To start with, I created a basic Windows form project. Created a form within the project and put a dataviewgrid on the form. The only change was to name the dataviewgrid “dgLists”.
Web References.
Next, I added a reference.specifically, a web reference. From the main Visual Studio menu, go to Project/Add Web Reference.
From the Add Web Reference dialog box, in the URL: area, put in http://<server>/_vti_bin/Webs.asmx (replacing <server> with your server name). Then click the Go button next to the URL: area. If everything goes correctly, you should se the “Web services found at this URL:” area say something like “1 Service Found” and – “Webs”.
Once you see this information, put something meaningful in the “Web reference name:” area. Something like wr<Server>Webs. (wr for web reference, replace <Server> with the server name, and Webs for what information is coming back.)
It is important to name this something meaningful. The name here is what will be used later in the code to get to the web reference.
Once all that is done, click the “Add Reference” button. This will add the web reference to your project. You can additionally add the other web references mentioned above, but for this example, we will just use the one.
The VB code.
Your form load code should be something like this:
Imports System.XmlImports System.NetImports System.Net.NetworkCredential Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim lMyWebs As New RevEWebs.Webs ' Set Network Credentials to be able to log into ' the SharePoint site lMyWebs.Credentials = New NetworkCredential("<UserName>", "<Password>", "<Domain>") ' Even though the Web Reference has this, ' the _vti_bin stuff is needed here again. lMyWebs.Url = "http://<server>/_vti_bin/webs.asmx" ' Actually get the list of sites from the ' <server> listed above Dim lMyWebsXMLNode = lMyWebs.GetWebCollection ' Set up an XML data document to read ' the returned list of webs from above Dim lMyXMLDataDocument As XmlDataDocument = New XmlDataDocument ' Set up a data set to load the grid ' on the main form Dim lMyDataSet As DataSet = lMyXMLDataDocument.DataSet ' Load the xsd file that has the layout ' of the XML lMyDataSet.ReadXmlSchema("Webs.xsd") ' Add an XML declaration to make the ' XmlNode a valid XML document lMyXMLDataDocument.LoadXml("<?xml version='1.0' ?>" & lMyWebsXMLNode.OuterXml) ' Display the result on the DataGrid dgLists.DataSource = lMyDataSet dgLists.DataMember = "Web"
End Sub
End Class
The XSD stuff.
The list of sub-webs from the web specified by <server> is in XML format. (On a side note, you can specify <server>/<sub-site> for <server> and it will bring back the list of sub-sites for <server>/<sub-site>.)
One additional bit of code you will need is an .xsd file with the layout of the XML. This is a requirement of the dataset, and not related to any SharePoint requirements.
Also, just a thing to know.to get a view of the XML, I set a breakpoint after the GetWebCollection line, and from the watch box, copied the OuterXML and pasted it into and XML editor. From there, I created the following .XSD file:
<?xml version="1.0" standalone="yes"?><xs:schema id="Lists" targetNamespace="http://schemas.microsoft.com/sharepoint/soap/" xmlns:mstns="http://schemas.microsoft.com/sharepoint/soap/" xmlns="http://schemas.microsoft.com/sharepoint/soap/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified"> <xs:element name="Webs" msdata:IsDataSet="true" msdata:EnforceConstraints="false"> <xs:complexType> <xs:choice maxOccurs="unbounded"> <xs:element name="Web"> <xs:complexType> <xs:attribute name="Title" form="unqualified" type="xs:string" /> <xs:attribute name="Url" form="unqualified" type="xs:string" /> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element></xs:schema>
You will need to save this in the same directory as the executable, i.e. <Project>inDebug<filename>.xsd. (In my case D:Development<Project>inDebugWebs.xsd.)
Run the project and you should see a form with a datagridview with the list of sub-sites and their associated URL's.
The Wrap Up.
The key to writing code to work with SharePoint sites, and have the code run on machines not hosting the SharePoint box seems to be Web Services. Again, if someone has the SharePoint Object Model doing this, PLEASE let me know.
While I have not investigated this too deeply, it seems that most everything that is possible to do in the SharePoint Object model is possible using the SharePoint Web Services. As I am forced to use the SharePoint Web Services to complete some projects that I have coming up, I'll post additional information on functionality. Again, if you have some insight on this, PLEASE let me know.
As always, the information and code provided here is for training purposes, provided with no guarantees, and use at your own risk. Additionally, feel free to use this stuff to get started with whatever you need.