Introduction
SharePoint is a recognized technology standard for company infrastructure organizations. It has wide functionality, and allows to store different information in one single place. However, sometimes this information needs to be accessed from an application. In this article, I will discuss the possibility to retrieve data from SharePoint using its WebServices. I will concentrate on the document libraries.
Background
SharePoint Server structures information by WebSites. Each WebSite has many Lists. Lists are used to store concrete data using ListItems: links to files, contacts, appointments, etc. ListItems have fields with necessary values. The user is able to customize these fields to store data. In the following section, I will show a general mechanism for retrieving data.
First of all, it is necessary to add a reference to SharePoint Services. They are accessible from the following URL: http://server-name/Site-Name/_vti_bin/service-name.asmx. For our purposes, we will use two of them: webs.asmx and lists.asmx.
Another important feature is that SharePoint Services are scope dependent, so if you want to retrieve a List from site S1, you should use the following WebSite: http://server-name/S1/_vti_bin/lists.asmx, and so on.
Retrieving WebSites
private static void GetSites(string url, string login, string password)
{
Webs service = new Webs();
service.PreAuthenticate = true;
service.Credentials = new System.Net.NetworkCredential(login, password);
service.Url = url + @"/_vti_bin/webs.asmx";
XmlNode sites = null;
try
{
sites = service.GetWebCollection();
}
catch
{
return;
}
foreach (System.Xml.XmlNode site in sites.ChildNodes)
{
Console.WriteLine(site.Attributes["Url"].Value);
GetLists(site.Attributes["Url"].Value, login, password);
GetSites(site.Attributes["Url"].Value, login, password);
}
}
Retrieving a WebSite List
private static void GetLists(string url, string login, string password)
{
Lists ls = new Lists();
ls.PreAuthenticate = true;
ls.Credentials = new NetworkCredential(login, password);
ls.Url = url + @"/_vti_bin/lists.asmx";
foreach (XmlNode list in ls.GetListCollection().ChildNodes)
{
if (Convert.ToInt32(list.Attributes["ServerTemplate"].Value) != 0x65)
{
continue;
}
string title = list.Attributes["Title"].Value;
string listUrl = list.Attributes["DefaultViewUrl"].Value.Replace(
"/Forms/AllItems.aspx", string.Empty);
char[] separator = new char[] { '/' };
string listPath = url.Substring(0, url.LastIndexOf('/'));
Console.WriteLine(listPath + listUrl + "/" + title);
AddListsItems(url, title, login, password);
}
}
Points of Interest
Sorry for being so concise. I'm quite new to SharePoint, but this task gave me some trouble. So I decided to post my solution.
The article will be further updated to show how to retrieve list items basing on SharePoint XML Queries.
History
- 12 June 2007: Added example application.
- 7 June 2007: First version.