tags:

views:

169

answers:

1

I want to check if a folder exists in sharepoint. But it could be any folder hosted in IIS.

Does Directory.Exists work? If not...

I found this method below, but it only creates the folder, no check to see if it already exists or not, does anyone have some working code to check if the folder exists?:

 private bool CreateFolder(string folderURL)

        {

            try

            {

                WebRequest request = WebRequest.Create(folderURL);

                request.Credentials = m_credentials;

                request.Method = "MKCOL";

                WebResponse response = request.GetResponse();

                response.Close();

                return true;

            }

            catch (WebException)

            {

                return false;

            }

        }
+4  A: 

No Directory.Exists does not work for URLs. As far as detecting the existance of the folder a GET, HEAD or PROPFIND request ought to be able to determine that.

AnthonyWJones
can you please provide some kind of working sample... can't find anything on the net
JL
JL: just send an HTTP request to the url, if you get a 404 then the URL doesn't exist.
Matt Ellen
@JL You practically have it in your question, change the Method to "HEAD" and check the Response status for 200 or 404.
AnthonyWJones
I figured out the answer... a PROPFIND is the most reliable way. HEAD and GET generate unexpected 401s in SharePoint at times.
JL
I've accepted the answer Anthony, but please change your answer to reflect that PROPFIND is the most reliable way to check if a resource exists in SharePoint.
JL