SharePoint Online: Get Email, Display Name, Login ID from "Created By" Column in PowerShell

Requirement: Get User Name, Email, Login ID, etc from "Created By" field value of a List Item using PowerShell

PowerShell to Retrieve User Object from "Created By" Field:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Variables for Processing
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$ListName = "Projects"
$ItemID = "1"

#Get Credentials to connect
$Cred = Get-Credential

Try { 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get the List Item
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $ListItem = $List.GetItemById($ItemId)
    $Ctx.Load($ListItem)
    $Ctx.ExecuteQuery()

    #Get the "Created By" column value of the Item
    $CreatedBy = $ListItem.FieldValues["Author"]

    #Get the User Object
    $User = $Ctx.Web.GetUserById($CreatedBy.LookupId)
    $Ctx.Load($User)
    $Ctx.ExecuteQuery()

    $User | Select Email, Title, LoginName   
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}
SharePoint Online: Get Email, Display Name, Login ID from "Created By" Column in PowerShell SharePoint Online: Get Email, Display Name, Login ID from "Created By" Column in PowerShell Reviewed by Salaudeen Rajack on May 30, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.