Monday, 9 March 2015

SharePoint 2013: How can I get a users SPPrincipal token?

In a SharePoint Claims authenticated environment, you need to extract the full claims token (not just DOMAIN\Username). Here are two simple methods using the ResolvePrincipal function to get the information:

PowerShell:

function GetUserPrincipalFromUsername($siteUrl, $login)
{
$web = Get-SPWeb $siteUrl
$principal = [Microsoft.SharePoint.Utilities.SPUtility]::ResolvePrincipal($web, $login, [Microsoft.SharePoint.Utilities.SPPrincipalType]::All, [Microsoft.SharePoint.Utilities.SPPrincipalSource]::All, $null, $false)
$web.Dispose()
return $principal
}

C#:

public SPPrincipalInfo GetUserPrincipalFromUsername(SPWeb web, string userName)
{
  return SPUtility.ResolvePrincipal(web, login, SPPrincipalType.All, SPPrincipalSource.All, null, false);
}

No comments:

Post a comment