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 = await session.GetFolderAsync(new Uri("http://server:8080/"));
IHierarchyItem[] children = await folder.GetChildrenAsync(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 = await session.GetFolderAsync(new Uri("http://server:8080/"));
IFolder newFolder = null;
try
{
    newFolder = await folder.CreateFolderAsync("My Folder");
}
catch (MethodNotAllowedException)
{
    Console.WriteLine("Folder already exists.");
}
if (newFolder != null)
    Console.WriteLine(newFolder.CreationDate.ToString());
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 = await session.GetFolderAsync(new Uri("http://server:8080/Library/"));
try
{
    await item.MoveToAsync(await item.GetParentAsync(), "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 = await session.GetFolderAsync(new Uri("http://server:8080/Library/ "));
bool isItemExists = await srcFolder.ItemExistsAsync("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 = await session.GetFolderAsync(new Uri("http://server:8080/Library/Sales"));
IFolder dstFolder = await session.GetFolderAsync(new Uri("http://server:8080/Library/"));
try
{
    await srcFolder.CopyToAsync(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 = await session.GetFolderAsync(new Uri("http://server:8080/Library/"));
IFolder itemDst = await session.GetFolderAsync(new Uri("http://server:8080/Sales/"));
try
{
    await itemSrc.MoveToAsync(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 = await session.GetFolderAsync(new Uri("http://server:8080/Sales/"));
try
{
    await folder.DeleteAsync();
}
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);
    }
}

Next Article:

Managing Custom Properties on a WebDAV Server Using .NET API