Archive for 'Blog'


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

Posted on January 13, 2011 by donabel

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//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;
}
else
{
     SPGroup group = web.Groups[lookup.LookupValue];
     ddlPoss.Items.FindByValue(group.ID.ToString()).Selected = true;
}

Comments are closed.

Upcoming Training Events


We also have courses on Ruby on Rails and iOS Development!

Check our training page for schedule, content and rates!

We are experts

Black Ninja Software was created with the idea that great software comes from great people. We are passionate about the technologies we use and continually refine our skills to better master what we do. This is what makes us Ninjas. We architect, design, and implement solutions using Microsoft Office SharePoint Server, SQL Server, and ASP.NET

We create software

If you have a business process that needs refining or automation, or you have a current project in distress that needs rescuing, we can help. Our wealth of experience will create your great idea from scratch if that's what you need ninjas to do for you. In addition to the work we do for our clients, we also have several of our own projects that are currently being developed. We use the same tools and the same skills on our projects as we do on yours.