Managing Custom Properties on a WebDAV Server Using .NET API

Enumerating Properties

string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFile file = await session.GetFileAsync(new Uri("http://server:8080/Library/1.txt"));

Property[] properties = await file.GetAllPropertiesAsync();
foreach (Property prop in properties)
{
    Console.WriteLine(prop.Name + " " + prop.StringValue);
}

Adding and Updating Properties

string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFile file = await session.GetFileAsync(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");

await file.UpdatePropertiesAsync(propsToAddAndUpdate, null);

Deleting Properties

string license = "<?xml version='1.0' encoding='u...
WebDavSession session = new WebDavSession(license);
session.Credentials = new NetworkCredential("User1", "pwd");
IFile file = await session.GetFileAsync(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");

await file.UpdatePropertiesAsync(null, propsToDelete);

Analyzing Which Properties Failed to Add/Update/Delete

try
{
    await file.UpdatePropertiesAsync(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);
    }
}

Next Article:

Uploading Files to WebDAV Server Using .NET API, C#