Extending profile field types for the Sitecore Active Directory module

The Sitecore Active Directory module provides integration between one or more Active Directory domains and Sitecore. Out-of-the-box this module supports reading and writing of data types such as: string, int and bool, however a common Active Directory data type byte[] is not supported.

Sitecore only deals with string types regardless of the original type in Active Directory. It does this by converting all types to and from string at runtime. There is no reason why types such as byte[] would be any different. Overwriting Sitecore's default behavior allows additional types to be supported whilst letting Sitecore handle all remaining types.

Enabling additional support for byte[] is simple enough:

  • Create a custom StringSettingsPropertyValue class that can handle getting/setting data from a byte[] field by encoding it as a Base64 string.
  • Override the default SettingsPropertyValueFactory to use the StringSettingsPropertyValue or fallback to the Sitecore implementation.
  • Update the Sitecore configuration to support the new SettingsPropertyValueFactory.

Create a custom StringSettingsPropertyValue

Create a ByteArraySettingsPropertyValue provider to read/write/convert data between byte[] and string.


namespace Sample.Business
{
    public class ByteArraySettingsPropertyValue : StringSettingsPropertyValue
    {
        public override object PropertyValue
        {
            get
            {
                return base.PropertyValue;
            }

            set
            {
                var byteArray = value as byte[];
                base.PropertyValue = byteArray != null ?
                    Convert.ToBase64String((byte[])value) :
                    string.Empty;
            }
        }
    }
}

Override the default SettingsPropertyValueFactory

Make Sitecore aware of the new StringSettingsPropertyValue by overriding the SettingsPropertyValueFactory. Ensuring the new byte array type is added to the switch statement.


using LightLDAP;
using LightLDAP.Diagnostic;

namespace Sample.Business
{
    public class SettingsPropertyValueFactoryExtended : SettingsPropertyValueFactory
    {
        public override ADSettingsPropertyValue GetSettingsPropertyValue(ADSettingsProperty property)
        {
            switch (property.ADType)
            {
                case "byte array":
                    return new ByteArraySettingsPropertyValue(property);

                default:
                    return base.GetSettingsPropertyValue(property);
            }
        }
    }
}

Update the Sitecore configuration

Now the provider exists to translate byte[] to string update the LDAP.config as shown below:

<setting name="LDAP.SettingsPropertyValueFactory" value="Sample.Business.SettingsPropertyValueFactoryExtended, Sample.Business" />  

Update any profile fields that require the byte[] type in the configuration as shown in the Profile Photo example below:

<add type="System.String" name="Photo" customProviderData="ad|byte array|thumbnailPhoto" />