Monday, March 28, 2016

Get Value from People Picker in SharePoint Online with PowerShell

When using SharePoint Online as an input for our PowerShell scripts, sometimes we need to get information from a People Picker type of field. Luckily this is pretty easy to do once you know where to look at.

First of all, you will need to use CSOM to get this property, as the Out of the Box SharePoint Online cmdlets to not allow you to do anything under the SPWeb. If you are an IT Pro, you might find PowerShell and CSOM a bit harder to understand and that’s normal. Chris O’Brien has a great blog post to get you started on Using CSOM in PowerShell scripts with Office 365

#First Connect to SharePoint Online
Import-Module Microsoft.Online.SharePoint.PowerShell
$username = "user@domain.com" 
$password = "Password"
$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force 
$url = "http://ift.tt/IVflO2"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
$context.Credentials = $credentials 
#Get the List and Items
$list = $Context.Web.Lists.GetByTitle("AddDistributionList")
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(1000) 
$items = $list.GetItems($query)
$context.Load($list)
$context.Load($items) 
$context.ExecuteQuery()
#Loop Trough the Items
foreach ($item in $items) {
#UserName is our Column Name of type People Picker
$User = $item["Username"].Email
Write-Host $User
}

Whenever we simply want a string from SharePoint Online, we simply use $item[“ColumnName”] and it works. However that returns nothing for a People Picker Column. For a People Picker Column, here is a screenshot of what properties look like.

Get Value from People Picker in SharePoint Online PowerShell

So if you need the Email, you simply use: item[“ColumnName”].Email . If you want the name, you would use $item[“ColumnName “].LookupValue

Get Value from People Picker in SharePoint Online PowerShell

I hope this was helpful! If you want to learn more about PowerShell for Office 365, check out my Pluralsight course by clicking the banner below!

Learn PowerShell for Office 365

The post Get Value from People Picker in SharePoint Online with PowerShell appeared first on Absolute SharePoint Blog by Vlad Catrinescu.


by Vlad Catrinescu via Absolute SharePoint Blog by Vlad Catrinescu

No comments:

Post a Comment