Published Friday, March 16, 2007 7:23 AM by martin

Extracting SharePoint Library Content via WebDAV

I wanted to write some C# code that could query the contents of a document library in SharePoint.  I couldn't find any clear indication of how to do this from a machine other than the SharePoint server itself, so I came up with my own approach using WebDAV.  There might be better ways to do this, but I thought I'd share anyway...

I put together an HTTP request with the URL of my document library, and specified a method of "PROPFIND".  I then copy the response, which is XML, into a string.  The following code fragment shows what I did: -

 

  // Get the library contents into xml

  WebRequest req = HttpWebRequest.Create(@"http://spserver/sites/demo/mylibrary");

  req.Method = "PROPFIND";

  req.UseDefaultCredentials = true;

  WebResponse resp = req.GetResponse();

  Stream str = resp.GetResponseStream();

  int len = (int)resp.ContentLength;

  byte[] buffer = new byte[len];

  str.Read(buffer, 0, len);

  str.Close();

  str.Dispose();

  resp.Close();

  ASCIIEncoding enc = new ASCIIEncoding();

  string xml = enc.GetString(buffer, 0, len);

 

The XML you get back from the server describes the library itself and all documents therein.  The following snippet is an example.  The first <D:response> element describes the library itself, and is followed by a separate <D:response> element for each document in the library.  Where you see ". . ." it means that I've chopped out a lot of XML for clarity, so you can see the structure.  What you actually get there are more properties that describe the item, such as date/time created, date/time last modified, etc.

 

<?xml version="1.0" encoding="utf-8" ?>

<D:multistatus

  xmlns:D="DAV:"

  xmlns:Office="urn:schemas-microsoft-com:office:office"

  xmlns:Repl="http://schemas.microsoft.com/repl/"

  xmlns:Z="urn:schemas-microsoft-com:">

 

    <D:response>

      <D:href>http://spserver/sites/demo/mylibrary</D:href>

      <D:propstat>

        <D:prop>

          <D:displayname>mylibrary</D:displayname>

 

          . . .

 

        </D:prop>

        <D:status>HTTP/1.1 200 OK</D:status>

      </D:propstat>

    </D:response>

 

    <D:response>

      <D:href>http://spserver/sites/demo/mylibrary/first.doc</D:href>

      <D:propstat>

        <D:prop>

          <D:displayname>first.doc</D:displayname>

 

          . . .

 

        </D:prop>

        <D:status>HTTP/1.1 200 OK</D:status>

      </D:propstat>

    </D:response>

 

    <D:response>

      <D:href>http://spserver/sites/demo/mylibrary/second.doc</D:href>

      <D:propstat>

        <D:prop>

          <D:displayname>second.doc</D:displayname>

 

          . . .

 

        </D:prop>

        <D:status>HTTP/1.1 200 OK</D:status>

      </D:propstat>

    </D:response>

 

</D:multistatus>

 

 

Technorati tags: , , ,