.NET Server

Creating a WebDAV Property Handler

In this article

Creating a WebDAV Property Handler

In this article, we will describe how to add a custom WebDAV property. The IT Hit WebDAV Server Engine supports two approaches to adding and updating custom properties. You can add a property inside IHierarchyItem.UpdatePropertiesAsync()GetPropertiesAsync() OR you can implement IPropertyHandler interface. 

Implementing IPropertyHandler Interface

This approach is preferred if you want to create several properties that belong to one namespace. Implementing the IPropertyHandler interface allows you to register a namespace that will be injected at the beginning of the XML document. Using this approach you also have complete control over the reading and writing the property XML and can customize the property to your needs. 

The namespace registration at the beginning of the WebDAV XML document and prefix parameter in the PropertyName class are provided in the IT Hit WebDAV Server Engine v11.1.XXXX and later versions. 

Here is a sample IPropertyHandler interface implementation:

public class MyPropertyHandler : IPropertyHandler
{
    public bool IsReadonly => true;

    public bool IncludeInAllProp => true;

    public bool AppliesTo(IHierarchyItem item)
    {
            return true; // Add this property to all items.
    }

    public Task UpdateAsync(ContextAsync<IHierarchyItem> context, IHierarchyItem item, XElement value)
    {
            throw new NotImplementedException();
    }

    public async Task WriteAsync(XmlWriter writer, IHierarchyItem item, ContextAsync<IHierarchyItem> context)
    {
            await writer.WriteElementStringAsync("myns", "PropName1", null, "Property Value");
    }
}

You can read/write multiple properties in one class derived from IPropertyHandler.

Then you must register the new interface before calling the DavEngineAsync.RegisterPropertyHandler() method, after creating the Engine instance. Note that you will typically create a single instance on the Engine per application and register the handler only one time after creating the Engine. After this, you can call DavEngineAsync.RunAsync() method multiple times, to process incoming requests.

To force the namespace registration at the be beginning of the WebDAV XML you must specify the namespace prefix when creating the PropertyName class:

RegisterPropertyHandler(new PropertyName("PropName1", "https://www.ithit.com/custom/", "myns"), new MyPropertyHandler());
RegisterPropertyHandler(new PropertyName("PropName2", "https://www.ithit.com/custom/", "myns"), new MyPropertyHandler());

or you can create several classes, each for its own property:

RegisterPropertyHandler(new PropertyName("PropName1", "https://www.ithit.com/custom/", "myns"), new MyPropertyHandler1());
RegisterPropertyHandler(new PropertyName("PropName2", "https://www.ithit.com/custom/", "myns"), new MyPropertyHandler2());