Managing Items Hierarchy on a WebDAV Server Using .NET API

Getting List of Child Items

string license = "<?xml version='1.0' encoding='u...
WebDavSessionAsync session = new WebDavSessionAsync(license);
session.Credentials = new NetworkCredential("User1", "pwd");

IFolderAsync folder = await session.OpenFolderAsync(new Uri("http://server:8080/"));
IHierarchyItemAsync[] children = await folder.GetChildrenAsync(false);
foreach (IHierarchyItemAsync item in children)
    Console.WriteLine(item.DisplayName);

Creating Folder

string license = "<?xml version='1.0' encoding='u...
WebDavSessionAsync session = new WebDavSessionAsync(license);
session.Credentials = new NetworkCredential("User1", "pwd");

IFolderAsync folder = await session.OpenFolderAsync(new Uri("http://server:8080/"));
IFolderAsync newFolder = null;
try
{
    newFolder = await folder.CreateFolderAsync("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...
WebDavSessionAsync session = new WebDavSessionAsync(license);
session.Credentials = new NetworkCredential("User1", "pwd");

IFolderAsync item = await session.OpenFolderAsync(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...
WebDavSessionAsync session = new WebDavSessionAsync(license);
session.Credentials = new NetworkCredential("User1", "pwd");

IFolderAsync srcFolder = await session.OpenFolderAsync(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...
WebDavSessionAsync session = new WebDavSessionAsync(license);
session.Credentials = new NetworkCredential("User1", "pwd");

IFolderAsync srcFolder = await session.OpenFolderAsync(new Uri("http://server:8080/Library/Sales"));
IFolderAsync dstFolder = await session.OpenFolderAsync(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 (IMultistatusResponseAsync 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...";
WebDavSessionAsync session = new WebDavSessionAsync(license);
session.Credentials = new NetworkCredential("User1", "pwd");

IFolderAsync itemSrc = await session.OpenFolderAsync(new Uri("http://server:8080/Library/"));
IFolderAsync itemDst = await session.OpenFolderAsync(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 (IMultistatusResponseAsync 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...";
WebDavSessionAsync session = new WebDavSessionAsync(license);
session.Credentials = new NetworkCredential("User1", "pwd");

IFolderAsync folder = await session.OpenFolderAsync(new Uri("http://server:8080/Sales/"));
try
{
    await folder.DeleteAsync();
}
catch (WebDavHttpException ex)
{
    Console.WriteLine(ex.Message + " " + ex.Status.Code + " " + ex.Status.Description);
    foreach (IMultistatusResponseAsync 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