Getting Started with WebDAV Client for .Net
Adding Reference to Your Project
Setting License
Authenticating
Getting a list of child items
Creating folder
Renaming Item
Reading properties
Adding and updating custom properties
Deleting custom properties
Analyzing which properties failed to add/update/delete
Checking if child item exists
Copying items
Moving items
Deleting items
Connection Limits
Connecting over SSL / HTTPS
Setting proxy connection
Adding Reference to Your Project
Download ITHit.WebDAV.Client.dll here and add reference to this dll to your .NET project. In Visual Studio select Add Reference context menu and select Browse tab in Add Reference dialog. Find the ITHit.WebDAV.Client.dll and click OK.

Then add following namespaces to your .cs file:
using ITHit.WebDAV.Client;
using ITHit.WebDAV.Client.Logger;
using ITHit.WebDAV.Client.Exceptions;
Setting License
The ITHit.WebDAV.Client.dll is fully functional and does not have any limitations. However to start using the API you will also need a license string. Download the trial license here. The license is time-limited and the dll will stop working after the trial period expires.
Pass the license text from a license file to WebDAVSession constructor:
string license = @"<?xml version='1.0' ...
WebDavSession session = new WebDavSession(license);
Make sure you do not change any text in Data and Signature tags of license string as license validation will fail.
Authenticating
The WebDAV client API supports all types of authentication supported by .Net Framework: basic, digest, negotiate, NTLM, Kerberos.
string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFolder folder = null;
try
{
folder = session.OpenFolder(new Uri("https://server/Products"));
}
catch(UnauthorizedException)
{
Console.WriteLine("Incorrect user name or password.");
}
catch(NotFoundException)
{
Console.WriteLine("Folder not found.");
}
if(folder!=null)
{
Console.WriteLine(folder.LastModified.ToString());
}
To authenticate against Active directory accounts use:
session.Credentials = new NetworkCredential("User1", "pwd", "DOMAIN");
To utilize the user name, password, and domain of the user who is currently logged in or impersonated use:
session.Credentials = CredentialCache.DefaultCredentials;
Getting a list of child items
string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFolder folder = session.OpenFolder( new Uri("http://server:8080/"));
IHierarchyItem[] children = folder.GetChildren(false);
foreach (IHierarchyItem item in children)
Console.WriteLine(item.DisplayName);
Creating folder
string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFolder folder = session.OpenFolder(new Uri("http://server:8080/"));
IFolder newFolder = null;
try
{
newFolder = folder.CreateFolder("My Folder");
}
catch(MethodNotAllowedException)
{
Console.WriteLine("Folder already exists.");
}
if (newFolder!=null)
Console.WriteLine(newFolder.CreationDate.ToString());
Renaming Item
string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFolder item = session.OpenFolder(new Uri("http://server:8080/Library/"));
try
{
item.MoveTo(item.GetParent(), "My new name", false);
}
catch (PreconditionFailedException)
{
Console.WriteLine("The folder with such name already exists and 'overwrite' was 'false'.");
}
Reading properties
string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IResource resource = session.OpenResource(new Uri("http://server:8080/Library/1.txt"));
Property[] properties = resource.GetAllProperties();
foreach(Property prop in properties)
{
Console.WriteLine(prop.Name + " " + prop.StringValue);
}
Adding and updating custom properties
string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IResource resource = session.OpenResource(new Uri("http://server:8080/Library/Products.docx"));
Property[] propsToAddAndUpdate = new Property[3];
propsToAddAndUpdate[0] = new Property(new PropertyName("Ammount", "Sales"), "1200");
propsToAddAndUpdate[1] = new Property(new PropertyName("ManagerApproved", "Sales"), "Yes");
propsToAddAndUpdate[2] = new Property(new PropertyName("Branch", "Sales"), "EMEA Region");
resource.UpdateProperties(propsToAddAndUpdate, null);
Deleting custom properties
string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IResource resource = session.OpenResource(new Uri("http://server:8080/Library/Products.docx"));
PropertyName[] propsToDelete = new PropertyName[3];
propsToDelete[0] = new PropertyName("Ammount", "Sales");
propsToDelete[1] = new PropertyName("ManagerApproved", "Sales");
propsToDelete[2] = new PropertyName("Branch", "Sales");
resource.UpdateProperties(null, propsToDelete);
Analyzing which properties failed to add/update/delete
try
{
resource.UpdateProperties(propsToAddAndUpdate, propsToDelete);
}
catch (PropertyException ex)
{
Console.WriteLine(ex.Message + " " + ex.Status.Code + " " + ex.Status.Description);
// Find which properties failed to add/update/delete
foreach (IPropertyMultistatusResponse propInfo in ex.Multistatus.Responses)
{
Console.WriteLine(propInfo.PropertyName + " " + propInfo.Status.Code + " " + propInfo.Status.Description);
}
}
Checking if child item exists
string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFolder srcFolder = session.OpenFolder(new Uri("http://server:8080/Library/ "));
bool isItemExists = srcFolder.ItemExists("image.gif");
if(isItemExists)
Console.WriteLine("Item exists");
else
Console.WriteLine("Item not found");
Copying items
string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFolder srcFolder = session.OpenFolder(new Uri("http://server:8080/Library/Sales"));
IFolder dstFolder = session.OpenFolder(new Uri("http://server:8080/Library/"));
try
{
srcFolder.CopyTo(dstFolder, "Test1", true, false);
}
catch (PreconditionFailedException)
{
Console.WriteLine("The item with such name exists and 'overwrite' was 'false'.");
}
catch (WebDavHttpException ex)
{
Console.WriteLine(ex.Message + " " + ex.Status.Code + " " + ex.Status.Description);
foreach (IMultistatusResponse resp in ex.Multistatus.Responses)
{ // Find which items failed to copy.
Console.WriteLine(resp.Href + " " + resp.Status.Code + " " + resp.Status.Description);
}
}
Moving items
string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFolder itemSrc = session.OpenFolder(new Uri("http://server:8080/Library/"));
IFolder itemDst = session.OpenFolder(new Uri("http://server:8080/Sales/"));
try
{
itemSrc.MoveTo(itemDst, itemSrc.DisplayName, false);
}
catch (PreconditionFailedException)
{
Console.WriteLine("The item with such name exists and 'overwrite' was 'false'.");
}
catch (WebDavHttpException ex)
{
Console.WriteLine(ex.Message + " " + ex.Status.Code + " " + ex.Status.Description);
foreach (IMultistatusResponse resp in ex.Multistatus.Responses)
{ // Find which items failed to move.
Console.WriteLine(resp.Href + " " + resp.Status.Code + " " + resp.Status.Description);
}
}
Deleting items
string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFolder folder = session.OpenFolder(new Uri("http://server/Sales/"));
try
{
folder.Delete();
}
catch (WebDavHttpException ex)
{
Console.WriteLine(ex.Message + " " + ex.Status.Code + " " + ex.Status.Description);
foreach (IMultistatusResponse resp in ex.Multistatus.Responses)
{ // Find which items failed to delete.
Console.WriteLine(resp.Href + " " + resp.Status.Code + " " + resp.Status.Description);
}
}
Connection limits
By default every .Net application can open only 2 connections with each domain. To open more connections simultaneously you must increase connection limit:
ServicePointManager.DefaultConnectionLimit = 40;
Connecting over SSL / HTTPS
If WebDAV server returns incorrect server certificate the API will throw WebDavException with a description “The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.” To avoid this exception and accept non-trusted server certificates set the ServerCertificateValidationCallback in ServicePointManager prior to calling client API methods:
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
...
private static bool ValidateCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
Console.WriteLine("Issued by: " + certificate.Issuer);
Console.WriteLine("Issued to: " + certificate.Subject);
Console.WriteLine("Valid until: " + certificate.GetExpirationDateString());
if (sslPolicyErrors == SslPolicyErrors.None)
Console.WriteLine("Valid server certificate");
return true;
}
...
ServicePointManager .ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateCertificate);
WebDavSession session = new WebDavSession(license);
IFolder item = session.OpenFolder(new Uri("https://server:8080/"));
Setting proxy connection
WebDavSession session = new WebDavSession(license);
WebProxy proxy = new WebProxy();
proxy.Address = new Uri("http://server1:8888");
proxy.Credentials = new NetworkCredential("User1", "pwd");
session.Proxy = proxy;
IFolder folder = session.OpenFolder(new Uri("http://server2:9876/"));
|