Subscribe via RSS Feed Connect with me on LinkedIn

How to Identify if SharePoint “Person or Group” Field Entry is User or Group

[ 0 ] January 13, 2011 |

In a lot of our custom forms, we use EnsureUser to verify if a user is exists in the SharePoint site or not and if not, EnsureUser takes care of setting them up for us. According to MS:

Checks whether the specified login name belongs to a valid user of the website, and if the login name does not already exist, adds it to the website.

When working with Person or Group columns, often times we use EnsureUser, however, if the Person or Group column you’re reading from could potentially contain a group, the EnsureUser call will fail. So we need a way to determine if what’s stored in a Person or Group column is a User or a Group entity.

This is what we came up with, it works well for us:

//SPPrincipalInfo provides access to information about the current principal.
SPPrincipalInfo principalInfo = SPUtility.ResolveWindowsPrincipal(site.WebApplication, lookup.LookupValue, SPPrincipalType.All, false);

//if this is null, then we assume it's a SharePoint group
if (principalInfo != null)
     //Checks whether the specified login name belongs to a valid user.
     SPUser user = web.EnsureUser(lookup.LookupValue);
     ddlPoss.Items.FindByValue(user.ID.ToString()).Selected = true;
     SPGroup group = web.Groups[lookup.LookupValue];
     ddlPoss.Items.FindByValue(group.ID.ToString()).Selected = true;

Tags: ,

Category: Blog

About Shereen Qumsieh: View author profile.

Leave a Reply

If you want a picture to show with your comment, go get a Gravatar.