Click here to Skip to main content
Click here to Skip to main content

Using SharePoint Web Services to Explore Document Libraries

By , 8 Jul 2007
 

Screenshot - Img.gif

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

//
// Get Web Sites
//
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

//
//Get Web Site Lists  
//
private static void GetLists(string url, string login, string password)
{
    //List WebService 
    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)
    {
        //Check whether list is document library
        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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Petro Protsyk
Architect
Netherlands Netherlands
Member
Please visit my website for more articles

I'm software developer & Ph.D. student

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
AnswerRe: dispaly images from sharepoint to websitememberT.Ashraf25 Nov '08 - 12:52 
Just point the Image sharepoint url and point it to the image url to sharepoint url.
 
Tanveer
WWW.TANVTECH.COM
SOFTWARE ENGINEER

GeneralRe: dispaly images from sharepoint to website [modified]membermangia26 Nov '08 - 6:56 
Yes, that works fine if you are behind the firewall, but I need to able read images from various client SharePoint sites and would need the proper security access rights. I expect many SharePoint installation are intranets and the Url might not be accessible. Is there a SharePoint web service method to fetch images and return as a serialized object?
 
Chris Mangiapane. MCSD
modified on Wednesday, November 26, 2008 1:54 PM

GeneralRe: dispaly images from sharepoint to websitememberT.Ashraf27 Nov '08 - 2:30 
You have to give access to your application in sharepoint or pass the access parameters to the sahrepoint using the sahrepoint web services.
 
Tanveer
WWW.TANVTECH.COM
SOFTWARE ENGINEER

GeneralThanks - This really kickstarted my work!memberChris Doherty30 Mar '08 - 14:43 
Err.... thanks?
GeneralWSS versionmemberT. Smaavik5 Sep '07 - 3:59 
Hi.
 
Which version of Sharepoint services is this example using? Is it 2.0?
I can't seem to find the PreAuthenticate and Credentials properties in WSS 3.0.
 
Does anyone know how to achieve the same the same in 3.0?
 
-Trond
GeneralRe: WSS versionmemberExtrim5 Sep '07 - 4:18 
This is WSS 3.0,
 
but PreAuthenticate and Credentials properties are properties of
Web Service which can be generated by Visual Studio.
GeneralA suggestion...memberHyperX10 Jun '07 - 4:05 
Good article...
 
A sample download of Web and Win solution would add value to this article as there are many beginners reading this and they wouldn't know where to start.
 
Thanks,
 
HyperX.
GeneralRe: A suggestion...memberExtrim10 Jun '07 - 20:15 
Thank you for your interest.
 
I will publish Win solution
this week as soon it will be good enough Smile | :)
 

 
All the best,
Piter

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 9 Jul 2007
Article Copyright 2007 by Petro Protsyk
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid