Start developing WSS 3.0 Webservice
My goal was to programmatically create a view on a document library. In my previous post I briefly described how to develop WSS using the Object Model directly.
In this post I wanna talk about the Web services wrapped around WSS. You can get more info in the WSS SDK or the online version here.
This link provides an overview of the different web services.
Lets start with a simple console example here.
static void Main(string[] args)
{
string listUrl = "http://bsend3/Docs/_vti_bin/Lists.asmx";
string list = "{834741CC-5486-4E29-86C9-E23F5126F53B}";
try
{
PrintLists(listUrl);
PrintListProps(list, listUrl);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error: " + ex.Message);
Console.WriteLine("Detail: " + ex.Detail.InnerText);
Console.ForegroundColor = ConsoleColor.White;
}
Console.ReadKey();
}
We do want to catch exceptions. Basically the exception that could occur when using the webservice is: 'Microsoft.SharePoint.SoapServer.SoapServerException'. However since I'm developing on a development machine and not on a server has WSS installed, my environment doesn't recognize this exception object. When we browse the documentation, we'll see that this exception is derived from the .NET known 'System.Web.Services.Protocols.SoapException'.
Below we'll print all SharePoint Lists retrieved from the List Webservice. Programming in .Net against a web service requires a web reference. Read this post for the specific WSS details. After we have a web reference (in our case its called 'list_ws') you can execute this code:
private static void PrintLists(string url)
{
Console.WriteLine("PrintLists");
Console.WriteLine("----------");
list_ws.Lists listService = new list_ws.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.Url = url;
XmlNode ndLists = listService.GetListCollection();
foreach (XmlNode ndList in ndLists)
{
Console.WriteLine("Title: " + ndList.Attributes["Title"].Value);
Console.WriteLine("ID: " + ndList.Attributes["ID"].Value);
Console.WriteLine("Desc: " + ndList.Attributes["Description"].Value);
Console.WriteLine("".PadRight(50, '-'));
Console.ReadKey();
}
}
When calling this function, we'll pass an url. This url is the place from where the GetListCollection will start looking for lists. In our case its on our WSS server (BSEND3) in the Docs subsite (this one exists in a default installation of SharePoint 2007). Note that the result is an Xml Node as described in the documentation.
In the next function we'll print the properties of one list:
static private void PrintListProps(string listId, string url)
{
Console.WriteLine("PrintListProps");
Console.WriteLine("--------------");
list_ws.Lists listService = new list_ws.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.Url = url;
XmlNode ndList = listService.GetList(listId);
Console.WriteLine("Title: " + ndList.Attributes["Title"].Value);
Console.WriteLine("ID: " + ndList.Attributes["ID"].Value);
Console.WriteLine("Desc: " + ndList.Attributes["Description"].Value);
Console.WriteLine("".PadRight(50, '-'));
Console.ReadKey();
}
The code of this function is quit similar with the previuos one, however we pass an additional listId. This is the Id of the List for which we'll want to display details for. Now this is in my opinion the hard part when working with the webservices. How do we retrieve the original IDs?
I figured, that there must be some tool to browse in the structure of a WSS/SharePoint environment and just easily retrieve the propper ID's. The original SharePointExplorer tool isn't updated to WSS 3.0 or SharePoint 2007 yet (and no, it doesn't work at all on the new versions, I tested it).
Another tool is SharePoint Inspector, but at the time of writing I couldn't download it.
So, today I just use the Webservice features like GetListCollection and look into the results to retrieve the ID's. I know, its not a very smart way, but I don't want to write an explorer tool when there are some other tools comming around the corner, right?
grtz