Managing Items Hierarchy on a WebDAV Server Using .NET API

Getting 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'.");
}

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