At some point in your journeys with PowerShell and SharePoint you'll need to pull a user from a list/library column and get an email address or login name, etc. If you've never done this before it can be a little frustrating as you have to jump through a couple of hoops. Let's take a look at simply querying the list.
Let's assume we have a library with a Name column and a user column called "Owner". Now we can easily query the library and pull out the "Name" with something like this:
# $docLib is a reference to a library and $spQuery is an SPQuery object with a CAML query
$spListItems = $docLib.GetItems($spQuery)
# Loop through items and write out info
foreach ($item in $spListItems) {
Write-Host $item.Name
}
In this example we simply query the library and loop through the results, writing out the "Name". But what if we needed to also write out the email address of the user column. Here is where the hoops come in, fortunately they are small.
You have to perform some casts in order to get the actual SPUser object. So taking the previous example, we can re-write it like this:
# $docLib is a reference to a library and $spQuery is an SPQuery object with a CAML query
$spListItems = $docLib.GetItems($spQuery)
# Loop through items and write out info
foreach ($item in $spListItems) {
Write-Host $item.Name
# Get the SPUser object from the column
$spFieldUser = [Microsoft.SharePoint.SPFieldUser]$item.Fields.GetField("Owner");
$spFieldUserValue = [Microsoft.SharePoint.SPFieldUserValue]$spFieldUser.GetFieldValue($item["Owner"].ToString());
$user = $spFieldUserValue.User;
# Pull out the user email
Write-Host = $user.Email}
In this example we first have to create an SPFieldUser object using $item.Fields.GetField("Owner"). Once we have that reference we can use the GetFieldValue method to return an SPFieldUserValue object, which will contain the actual SPUser object. Now we have access to all the properties on the user like Email, DisplayName, LoginName, etc.
As you can see, its pretty easy to get to the user object in SharePoint via PowerShell.
Enjoy!
Let's assume we have a library with a Name column and a user column called "Owner". Now we can easily query the library and pull out the "Name" with something like this:
# $docLib is a reference to a library and $spQuery is an SPQuery object with a CAML query
$spListItems = $docLib.GetItems($spQuery)
# Loop through items and write out info
foreach ($item in $spListItems) {
Write-Host $item.Name
}
In this example we simply query the library and loop through the results, writing out the "Name". But what if we needed to also write out the email address of the user column. Here is where the hoops come in, fortunately they are small.
You have to perform some casts in order to get the actual SPUser object. So taking the previous example, we can re-write it like this:
# $docLib is a reference to a library and $spQuery is an SPQuery object with a CAML query
$spListItems = $docLib.GetItems($spQuery)
# Loop through items and write out info
foreach ($item in $spListItems) {
Write-Host $item.Name
# Get the SPUser object from the column
$spFieldUser = [Microsoft.SharePoint.SPFieldUser]$item.Fields.GetField("Owner");
$spFieldUserValue = [Microsoft.SharePoint.SPFieldUserValue]$spFieldUser.GetFieldValue($item["Owner"].ToString());
$user = $spFieldUserValue.User;
# Pull out the user email
Write-Host = $user.Email}
In this example we first have to create an SPFieldUser object using $item.Fields.GetField("Owner"). Once we have that reference we can use the GetFieldValue method to return an SPFieldUserValue object, which will contain the actual SPUser object. Now we have access to all the properties on the user like Email, DisplayName, LoginName, etc.
As you can see, its pretty easy to get to the user object in SharePoint via PowerShell.
Enjoy!