Tuesday, February 16, 2016

PowerShell-CAML SPQuery to Filter List Items based of Person or Group Field Value

In a project tracking SharePoint list, want to quickly get the list the projects where a particular user is listed under Team members - person or group (People picker) field.

Solution: Use SPQuery with PowerShell as in the below example.

PowerShell to Filter List Items based of Person or Group Field:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration parameters
$SiteURL = "http://portal.crescent.com/projectpipeline/"
$ListName = "Projects"
$FieldName= "TeamMembers"

#Get the User Account
$User = Get-SPUser -Identity $UserAccount -Web $SiteURL

#Query to filter List Items which contains user account
$SPQuery = new-object Microsoft.SharePoint.SPQuery
$Query =  "<Where><Eq><FieldRef Name='TeamMembers' LookupId='TRUE'/><Value Type='User'>$($User.ID)</Value></Eq></Where>"

#Get site and List objects
$web = Get-SPWeb $SiteURL
$List = $web.Lists.TryGetList($ListName)

#Filter List Items by Query
$ListItems = $List.GetItems($SPQuery)
foreach($item in $ListItems)
    write-host $Item["ProjectName"]
write-host "Total Number of Projects Found:" $ListItems.Count

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

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...