WebDav如何检查文件夹是否存在?

发布于 2024-08-16 11:28:14 字数 1287 浏览 5 评论 0原文

这是我当前的功能如下。它用于在 SharePoint 的文档库中创建文件夹,但使用 web dav 功能,这比 MOSS 的东西更容易。

我需要找到一种方法来可靠地确定该文件夹是否已经存在...请注意,现在我依赖于 try catch,但这意味着任何协议异常都不会抛出错误,因此它不是一个可靠的函数。如何使用 web dav 检查文件夹是否存在?

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;
                }
            }
        }

本质上我想要这个产品在这里实现的未包装版本: http://www.independentsoft.de/webdav/tutorial/exists.html

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南…巷孤猫 2024-08-23 11:28:14

如果您对 url 执行 PROPFIND,如果该文件夹不存在,您将收到 404 错误消息。

使 PROPFIND 看起来像这样(仅显示相关标头)

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

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

404 表示资源不存在,207 表示资源存在。

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.

一指流沙 2024-08-23 11:28:14

PROPFIND 是您的朋友:DAV:resourcetype 属性 (http://greenbytes .de/tech/webdav/rfc4918.html#rfc.section.15.9) 有一个用于集合的 DAV:collection 子元素。只需使用 PROPFIND 和 DAV:allprop 或 DAV:prop 检索它(两者均在 http://greenbytes.de/tech/webdav/rfc4918.html#rfc.section.9)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文