Thursday, 1 January 2015

SharePoint People picker - how to identity user or group in people and group column through code


using Microsoft.SharePoint.Utilities;


SPFieldUserValueCollection divisionUsers = (SPFieldUserValueCollection)lstItem["DivisionGroup"];

foreach (SPFieldUserValue divisionUser in divisionUsers)
{
try
{
       SPPrincipalInfo spPrincialInfo = SPUtility.ResolveWindowsPrincipal(spWeb.Site.WebApplication, divisionUser.LookupValue, SPPrincipalType.All, false);
       SPRoleAssignment roleAssignment = null;
if (spPrincialInfo == null)
{
//Principal is a group

}
else
                    {
//Principal is a user
}

}

We can use one of below 2 method for this :

SPUtility.ResolveWindowsPrincipal only resolve Windows user accounts or Groups.

SPUtility.ResolvePrincipal resolve both Windows User and group as well as Sharepoint Users and Groups.