I have a complex application that have several field of type UserMulti. These fields can accept either SPUser or SPGroup or a combination of both kind.
I have in my code the ID and the name of a SPPrincipal object. How can I get the corresponding SPPrincipal object?
I have this code :
public static SPPrincipal GetPrincipal(SPWeb web, int principalID, string principalName)
{
return web.AllUsers.Cast<SPPrincipal>().SingleOrDefault(u => u.ID == principalID && u.Name == principalName) ??
web.SiteGroups.Cast<SPPrincipal>().SingleOrDefault(g => g.ID == principalID && g.Name == principalName);
}
But that looks a bit weird to my eyes.
PS: I have ids and names because my value are coming from SPMetal, where fields of type UserMulti are created as one IList<int?> userIds and IList<string> userImnName
[Edit] I just realized that both SPGroup and SPUser are stored in the UserInformationList. So I can rely on Ids, I don't have to test the name.
My code is then slightly simplified :
public static SPPrincipal GetPrincipal(SPWeb web, int principalID, string principalName)
{
return web.AllUsers.Cast<SPPrincipal>().SingleOrDefault(u => u.ID == principalID) ??
web.SiteGroups.Cast<SPPrincipal>().SingleOrDefault(g => g.ID == principalID);
}