Share via


PowerShell Query SharePoint List Based on the View


PowerShell Query SharePoint List Based on the View


Summary

This is Wiki is based on the TechNet Forum Post.
Forum Post Link

Background

Code used by OP will query only default view - That's SharePoint default behaviour whenever we do query using PowerShell it will fetch the information from Default View

Solution

We should tell PowerShell explicitly to query the required view

PowerShell Code

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
function Get-SPSite([string]$url) {

New-Object Microsoft.SharePoint.SPSite($url)
}

function Get-SPWeb([string]$url) {

$SPSite = Get-SPSite $url
return $SPSite.OpenWeb()
$SPSite.Dispose()
}

function Get-SPList([string]$url, [string]$List) {

$OpenWeb = Get-SPWeb $url
$OpenList = $OpenWeb.Lists[$List]

return $OpenList

$OpenWeb.Dispose()
}

$url = "http://localhost"
$List = "EmployeeBirthdays"
$View = "Homepage"

$BirthdaysList = Get-SPList -url $url -List $List

foreach($EmpName in $BirthdaysList.Items) 
{
$EmpName.Title #---->displays all titles, which is all users
}

Fix

001
002
003
004
005
006
007
#Method 1
$viewName = "View name" 
$view = $list.Views[$viewName]

#Method 2
$viewId = "A6932332-765A-4776-A1DF-18016DA1D645" 
$view = $list.GetView($viewId)