tags:

views:

460

answers:

2

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

A: 

PROPFIND is your friend.

Julian Reschke
Could you possibly provide an example... I need something I cam implement fairly quickly... thanks...
JL
Protocol examples are in RFC 4918, Section 9.1.
Julian Reschke
+1  A: 

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.

Evert
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).
Julian Reschke
thnx I made those edits
Evert
How can I practically implement this in C#?
JL
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