As mentioned in the title I'm using the Principal Extensions of the Account Management API for getting and setting custom AD User attributes. Two of these custom attributes are string array. The problem im encountering right now is that I get an error when ever I'm trying to set a property as an empty array.
My Extended UserPrincipal looks like this:
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("user")]
class UserPrincipalEx : UserPrincipal
{
public UserPrincipalEx(PrincipalContext context) : base(context) { }
public UserPrincipalEx(PrincipalContext context, string samAccountName, string password, bool enabled):base(context, samAccountName, password, enabled) { }
[DirectoryProperty("course")]
public string[] course
{
get
{
int len = ExtensionGet("course").Length;
string[] courses = new string[len];
object[] coursesRaw = ExtensionGet("course");
for (int i = 0; i < len; i++)
{
courses[i] = (string)coursesRaw[i];
}
return courses;
}
sset
{
this.ExtensionSet("course", value);
}
}
[DirectoryProperty("interested")]
public string[] interested
{
get
{
int len = ExtensionGet("interested").Length;
string[] interested = new string[len];
object[] interestedRaw = ExtensionGet("interested");
for (int i = 0; i < len; i++)
{
interested[i] = (string)interestedRaw[i];
}
return interested;
}
set
{
this.ExtensionSet("interested", value);
}
}
public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue)
{
return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue);
}
public static new UserPrincipalEx FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
{
return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityType, identityValue);
}
}
And I'm trying to set the properties like this:
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, Settings.Default.DCName))
{
UserPrincipalEx exUser = UserPrincipalEx.FindByIdentity(pc, newMailUser.Name);
exUser.interested = CourseInterested.ToStringArray(newMailUser.CInterested);
exUser.course = CourseVisited.ToStringArray(newMailUser.CVisited);
exUser.Save(pc);
}
The ToStringArray Methods return a string array with values or an empty one. The code fails when newMailUser.CInterested or newMailUser.CVIsited has no entries and an empty string array is returned with the error message "Collections whoes elements are another collection cannot be set by ExtensionClasses." and further the error says that it occured at the set methode of the extended property ExtensionSet("property", value)
I used this microsoft example. I also tried this approach but sadly it didn't work for me.
If anyone would be so kind to help I would appreciate it.