I am trying to add a Contact
to a distribution list.
Here is how I am going about it:
public void AddContactsToGroup(string groupName, string[] userNames)
{
using (PrincipalContext context = GetPrincipalContext())
{
var group = GroupPrincipal.FindByIdentity(context, IdentityType.Name, groupName);
if (group.IsSecurityGroup.HasValue && group.IsSecurityGroup.Value == true)
throw new ArgumentException("Groups cannot be security groups.");
foreach (var userName in userNames)
{
var query = new ContactPrincipal(context)
{
Name = userName
};
var user = Search(query).First();
group.Members.Add(user);
}
group.Save(context);
}
}
private List<T> Search<T>(T query) where T : Principal
{
var searcher = new PrincipalSearcher();
searcher.QueryFilter = query;
PrincipalSearchResult<Principal> results = searcher.FindAll();
return results.Cast<T>().ToList();
}
Here is the ContactPrincipal
class (I borrowed this from somewhere):
[DirectoryObjectClass("contact")]
[DirectoryRdnPrefix("CN")]
public class ContactPrincipal : AuthenticablePrincipal
{
public ContactPrincipal(PrincipalContext context)
: base(context)
{
}
public static ContactPrincipal FindByIdentity(PrincipalContext context, string identityValue)
{
return (ContactPrincipal) Principal.FindByIdentityWithType(context, typeof (ContactPrincipal), identityValue);
}
public static ContactPrincipal FindByIdentity(PrincipalContext context, IdentityType identityType,
string identityValue)
{
return
(ContactPrincipal)
Principal.FindByIdentityWithType(context, typeof (ContactPrincipal), identityType, identityValue);
}
[DirectoryProperty("mail")]
public string EmailAddress
{
get
{
if (ExtensionGet("mail").Length == 1)
{
return ExtensionGet("mail")[0].ToString();
}
else
{
return null;
}
}
set { ExtensionSet("mail", value); }
}
[DirectoryProperty("givenName")]
public string GivenName
{
get
{
if (ExtensionGet("givenName").Length == 1)
{
return ExtensionGet("givenName")[0].ToString();
}
else
{
return null;
}
}
set { ExtensionSet("givenName", value); }
}
[DirectoryProperty("middleName")]
public string MiddleName
{
get
{
if (ExtensionGet("middleName").Length == 1)
{
return ExtensionGet("middleName")[0].ToString();
}
else
{
return null;
}
}
set { ExtensionSet("middleName", value); }
}
[DirectoryProperty("sn")]
public string Surname
{
get
{
if (ExtensionGet("sn").Length == 1)
{
return ExtensionGet("sn")[0].ToString();
}
else
{
return null;
}
}
set { ExtensionSet("sn", value); }
}
[DirectoryProperty("mobile")]
public string MobileTelephoneNumber
{
get
{
if (ExtensionGet("mobile").Length == 1)
{
return ExtensionGet("mobile")[0].ToString();
}
else
{
return null;
}
}
set { ExtensionSet("mobile", value); }
}
[DirectoryProperty("telephoneNumber")]
public string VoiceTelephoneNumber
{
get
{
if (ExtensionGet("telephoneNumber").Length == 1)
{
return ExtensionGet("telephoneNumber")[0].ToString();
}
else
{
return null;
}
}
set { ExtensionSet("telephoneNumber", value); }
}
}
My problem is, when I hit the
group.Members.Add(user)
line in the AddContactsToGroup
method, an error is thrown that states
The Principal object must have a valid SID IdentityType in order to perform this operation.
When I interrogate the properties of the ContactPrincipal
, the Sid
is, in fact null. This isn't surprising since a Contact
is an object with no security.
How can I add a Contact
to a non-security group?