Managing Custom Properties with JavaScript WebDAV Library

Using the library you can manage custom properties on a WebDAV server. Custom properties are supported by any Class 1 WebDAV server. Each custom property is represented by Property class and has name and value. Name of the property is represented by PropertyName class and has name and namespace.

Requesting Custom Properties When Listing Folder Content

When listing folder content you can request any custom properties from server. Pass the list of required properties in Folder.GetChildrenAsync() function call as a second parameter. In this case the properties will be returned for each item in the list and will become available in HierarchyItem.Properties list. 

var webDavSession = new ITHit.WebDAV.Client.WebDavSession();
webDavSession.OpenFolderAsync('http://localhost:87654/', null, function(oFolderAsyncResult) {

    /** @typedef {ITHit.WebDAV.Client.Folder} oFolder */
    var oFolder = oFolderAsyncResult.Result;

    var oPropName1 = new ITHit.WebDAV.Client.PropertyName('myname1', 'mynamespace');
    var oPropName2 = new ITHit.WebDAV.Client.PropertyName('myname2', 'mynamespace');

    oFolder.GetChildrenAsync(null, [oPropName1, oPropName2], function(oItemsAsyncResult) {

        /** @typedef {ITHit.WebDAV.Client.HierarchyItems[]} aItems */
        var aItems = oItemsAsyncResult.Result;

        for (var i = 0, l = aItems.length; i < l; i++) {
            var item = aItems[i];
            console.log(item.DisplayName);
            console.log(item.Properties.Find(oPropName1, false));
            console.log(item.Properties.Find(oPropName2, false));
        }
    }); 

});

To find the required property in the list of properties use PropertyList.Find() function. Note that this function does not make a server request, it just searches inside Properties list. 

To test if the property is present in the Properties list use PropertyList.Has() function. it returns true if the property exists in the list or false otherwise.

Enumerating Custom Properties

To request all custom properties associated with a file or folder from server use HierarchyItem.GetAllPropertiesAsync function.

var webDavSession = new ITHit.WebDAV.Client.WebDavSession();
var sFileAbsolutePath = 'http://localhost:87654/myfile.txt';
var fCallback = function() {};

webDavSession.OpenFileAsync(sFileAbsolutePath, null, function(oFileAsyncResult) {

    /** @typedef {ITHit.WebDAV.Client.File} oFile */
    var oFile = oFileAsyncResult.Result;

    oFile.GetAllPropertiesAsync(function(oAsyncResult) {

        /** @typedef {ITHit.WebDAV.Client.Property[]} aProperties */
        var aProperties = oAsyncResult.Result;

        for (var i = 0, l = aProperties.length; i < l; i++) {
            console.log(aProperties[i].Name + ': ' + aProperties[i].StringValue());
        }

        fCallback(oAsyncResult);
    });
});

Adding and Updating Custom Properties

To add or update custom properties use HierarchyItem.UpdatePropertiesAsync function. Pass the array of properties as a first parameter.

var webDavSession = new ITHit.WebDAV.Client.WebDavSession();
var sFileAbsolutePath = 'http://localhost:87654/myfile.txt';
var fCallback = function() {};

webDavSession.OpenFileAsync(sFileAbsolutePath, null, function(oFileAsyncResult) {

    /** @typedef {ITHit.WebDAV.Client.File} oFile */
    var oFile = oFileAsyncResult.Result;

    var oPropertyName = new ITHit.WebDAV.Client.PropertyName('myname', 'mynamespace');
    var oProperty = new ITHit.WebDAV.Client.Property(oPropertyName, 'Test value');

    oFile.UpdatePropertiesAsync([oProperty], null, null, function(oAsyncResult) {

        if (oAsyncResult.IsSuccess) {
            console.log('Property `mynamespace:myname` successfully added to file!');
        }

        fCallback(oAsyncResult);
    });
});

Deleting Custom Properties

To delete custom properties pass array of properties as a second parameter to  HierarchyItem.UpdatePropertiesAsync function.

var webDavSession = new ITHit.WebDAV.Client.WebDavSession();
var sFileAbsolutePath = 'http://localhost:87654/myfile.txt';
var fCallback = function() {};

webDavSession.OpenFileAsync(sFileAbsolutePath, null, function(oFileAsyncResult) {

    /** @typedef {ITHit.WebDAV.Client.File} oFile */
    var oFile = oFileAsyncResult.Result;

    var oPropertyName = new ITHit.WebDAV.Client.PropertyName('myname', 'mynamespace');

    oFile.UpdatePropertiesAsync(null, [oPropertyName], null, function(oAsyncResult) {

        if (oAsyncResult.IsSuccess) {
            console.log('Property `mynamespace:myname` successfully deleted from file!');
        }

        fCallback(oAsyncResult);
    });
}); 

Next Article:

Locking Items with JavaScript WebDAV Library