Sunday, January 8, 2017

Get User Account By Display Name in SharePoint

Requirement: Get user by display name in SharePoint
We have a CSV file with list of projects and their Team leads information. This list to be updated on a SharePoint list. But the challenge is 'Team Lead' field has display names of users instead of account name (Domain\LoginID). So prior updating to SharePoint list, we need the Login ID of the user from his display name.

Solution: Lets Query Active directory for the given display name to get the user's Login ID.

Prerequisites: You need to have PowerShell module for Active directory installed to use: Import-Module ActiveDirectory! Use PowerShell cmdlet: Add-WindowsFeature RSAT-AD-PowerShell to add AD Module for PowerShell to your server/desktop! Otherwise, you'll get an error message: "Import-Module : The specified module 'ActiveDirectory' was not loaded because no valid module file was found in any module directory"

PowerShell script to get user accounts from display name and update SharePoint List:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Import-Module ActiveDirectory

#Configuration Variables
$SiteURL = "http://intranet.crescent.com/"
$ListName = "Projects"                
$FieldName="Team Lead"
$CSVFile ="C:\TeamLeads.csv" 

#Custom Function Get User Account from Display Name in AD 
Function Get-UserAccount($DisplayName)
{
    $UserAccount=Get-ADUser -LDAPFilter "(displayName=$DisplayName)" | Select sAMAccountName
    if($UserAccount.sAMAccountName -ne $null)
    {
        return $UserAccount.sAMAccountName.tostring()
    }
    else
    {
        write-host $DisplayName not found in AD! -f Red
        return $null
    }
} 
#Import from CSV file - CSV has Headers ("ProjectName", "TeamLead")
$CSVData = Import-CSV -path $CSVFile

#Get the Target Web & List
$Web = Get-SPWeb -identity $WebURL
$List = $web.Lists[$ListName]
 
#Iterate through each Row in the CSV file
foreach ($Row in $CSVData) 
{
    #Filter by Project Name
 $Item = $List.Items | Where-Object { $_["Project Name"] -eq $Row.ProjectName }
  
 #If the matching project found
    If($Item -ne $null)
    {
  write-host "Searching for:"$Row.TeamLead
  #Get the User Account from Display Name
  $UserAccount = Get-UserAccount $Row.TeamLead
  
  #If User account found in AD
        if($UserAccount -ne $null)
        {
            $TeamLead=$web.ensureuser($UserAccount)
     
      #Update Team member field
   $item["Team Lead"] = $TeamLead
   $item.Update()
         Write-Host "Updated Project:"$Row.ProjectName -ForegroundColor Green
  }
  else
  {
   write-host "No matching User Account Found for :"$Row.TeamLead -f Red
  }
    }
    else
    {
        write-host "No matching List Item Found for:"$Row.ProjectName -f Red
    }  
}
Using SPUtility's ResolvePrincipal Method: Instead of querying Active directory, you can also use SPUtility's ResolvePrincipal method to get a user by display name:
$Web = Get-SPWeb "http://intranet.crescent.com"
$DisplayName="Barunx Romeih"
$Principal = [Microsoft.SharePoint.Utilities.SPUtility]::ResolvePrincipal($web, $DisplayName, "All", "All", $null, $false)
$Principal.LoginName



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...