SharePoint 2007, how to check if a folder exists in a document library

I am accessing SharePoint via its web services... Which are a bit limited, as a result I have turned to WebDav to perform some create folder functionality...

I have a document library, and I am about to create a folder using webdav, but I can't find any documentation on the internet or anywhere else on how to check if a folder already exists using webdav, so is there a way to check if a folder exists in a document library in SharePoint, any hack and slash methods welcome!

-------------Problems Reply------------

Somehow, I don't get your question. First sentence states you are using web service (I'd normally understand it as the SOAP web services provided by SharePoint). The next one says you are using WebDAV which is a completely different protocol.

So, WebDAV is the protocol "Windows Explorer" uses to access SharePoint, if you open it it "Explorer mode". Since all these requests are actually HTTP requests, you can spy on them, using the "Fiddler" tool.

I believe, before opening a folder, Windows Explorer tries to query sharepoint, if such folder exists. If I try to open an unexistant path \\mysrv\sites\myweb\mylib\notthere (but \\mysrv\sites\myweb\mylib is an existing document library!) thru windows explorer, the last HTTP call I see is:

PROPFIND /sites/myweb/mylib HTTP/1.1
User-Agent: Microsoft-WebDAV-MiniRedir/6.1.7600
Depth: 1
translate: f

Where SharePoint responds with: a list of subfolders and pages in this folder (very long XML, but it contains items like this):

<D:status>HTTP/1.1 200 OK</D:status>
<!---List of other <D:response> elements -->

If the contained element is a folder, it must have "D:isFolder" value "t". This way you can find, if the parent folder contains the folder you are going to create.

EDIT: created a small c# sample which first reads the result stream and then parses the result a bit. You need to make it better, to see if the list contains the folders you need or not.

System.Net.HttpWebRequest oReq;
string sUrl = "http://yoursite/sites/somesite/DocumentLibrary";
oReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(sUrl);

oReq.Method = "PROPFIND";
oReq.Credentials = System.Net.CredentialCache.DefaultCredentials;
oReq.AllowAutoRedirect = true;
oReq.UserAgent = "Microsoft-WebDAV-MiniRedir/6.1.7600";

//this causes all of the items to be enumerated,
//if it is 0, only the folder itself is returned in the response
oReq.Headers["Depth"] = "1";
oReq.Headers["translate"] = "f";
System.IO.StreamWriter oRequest =
new System.IO.StreamWriter(oReq.GetRequestStream());
System.IO.StreamReader oResponse =
new System.IO.StreamReader(oReq.GetResponse().GetResponseStream());
string sResponse = oResponse.ReadToEnd();

//done with the webclient stuff, check the results

System.Xml.XmlDocument oMyDoc = new System.Xml.XmlDocument();
System.Xml.XmlNamespaceManager oNsMgr =
new System.Xml.XmlNamespaceManager(oMyDoc.NameTable);
oNsMgr.AddNamespace("D", "DAV:");

System.Xml.XmlNodeList oAllResponses =
oMyDoc.SelectNodes(".//D:multistatus/D:response", oNsMgr);

foreach (System.Xml.XmlNode oNode in oAllResponses)
Console.WriteLine("Name: " +

if (oNode.SelectNodes("./D:propstat/D:prop/D:isFolder", oNsMgr).Count > 0)
Console.WriteLine("Is folder: " +
Console.WriteLine("Is folder: f");

YOu don;t need to, if it already exists, trying to create a new folder with that name will "silently" return the already existing folder.

Category:sharepoint Views:1 Time:2009-12-22
Tags: sharepoint wss

Related post

Copyright (C), All Rights Reserved.

processed in 0.177 (s). 11 q(s)