1

This is my current function below. Its used to create a folder in a document library in SharePoint but using web dav functionality, which is easier than MOSS stuff.

I need to find a way to determine reliably if the folder already exists... Notice now I am relying on that try catch, but this means that ANY protocol exception will not throw an error, so its not a reliable function. How can I check using web dav if a folder exists?

private void createFolderUsingWebDav(string siteAddress, string listAddress, string folderName)
        {
            //Check Databox Folder Exists
            string folderAddress = siteAddress + @"/" + listAddress + @"/" + folderName; 
            HttpWebResponse response;
            try
            {
                HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create(folderAddress);
                request.Credentials = wsLists.Credentials; // CredentialCache.DefaultCredentials;
                request.Method = "MKCOL";
                response = (System.Net.HttpWebResponse)request.GetResponse();
                response.Close();
            }
            catch (WebException ex)
            {
                if (ex.Status != WebExceptionStatus.ProtocolError)
                {
                    throw ex;
                }
            }
        }

Essentially I want the unwrapped version of what this product achieves here: http://www.independentsoft.de/webdav/tutorial/exists.html

2 Answers 2

5

If you do a PROPFIND on the url, you will get a 404 back if the folder does not exist.

Make the PROPFIND look something like this (only showing the relevant headers)

PROPFIND /yourfolder HTTP/1.1
Content-Type: application/xml

<?xml version="1.0"?>
<propfind xmlns="DAV:">
   <prop>
      <resourcetype />
   </prop>
</propfind>

404 means the resource doesn't exist, 207 means it does.

8
  • 207, not 217. See RFC 4918. To just check for the presence of a folder, you also want to only get the DAV:resourcetype property, not all properties (which can be much more expensive). Dec 23, 2009 at 9:02
  • How can I practically implement this in C#?
    – JL.
    Jan 5, 2010 at 11:25
  • I'm not a C# person, but I would expect it to be extremely similar to your MKCOL request. Just add the xml body and Content-Type header and you should be set.
    – Evert
    Jan 5, 2010 at 14:46
  • I think, you should also add Depth : 0 header for performance. Mar 5, 2013 at 9:07
  • 1
    @AdrianBaker if you are using a server that emits a 404 to indicate that the server is not ready to serve requests, then it's a buggy server. This is non-standard, don't do this.
    – Evert
    Mar 5, 2018 at 20:38
0

PROPFIND is your friend: the DAV:resourcetype property (http://greenbytes.de/tech/webdav/rfc4918.html#rfc.section.15.9) has a DAV:collection child element for collections. Just retrieve it using PROPFIND with DAV:allprop or DAV:prop (both described in http://greenbytes.de/tech/webdav/rfc4918.html#rfc.section.9).

2
  • Could you possibly provide an example... I need something I cam implement fairly quickly... thanks...
    – JL.
    Dec 22, 2009 at 18:30
  • Protocol examples are in RFC 4918, Section 9.1. Dec 23, 2009 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.