PowerShell Script to retrieve every and only List / Library items in a specific view in SharePoint 2010
-
04-10-2020 - |
Question
I am trying to iterate over each item in specific view for a list in SharePoint 2010.
What i have so far works to return items in the view, but only the items in the first paginated view for that set. So even though all Documents contains 11K items i only get 30 returned to me.
I found this question and it was answered, but the code there doesnt work for me: Powershell Script to get items only in a particular view of SharePoint List/library
Here is what i am trying:
$web = Get-SPWeb http://myweb
$list = $web.Lists["MyList"]
$view = $list.Views["MyViewName"]
$items = $list.GetItems($view)
$cntItems = 0
foreach($item in $items)
{
$cntItems++
}
write-host $cntItems
The above returns 30
since that is what the view has as its page item limit.
30
Is there some setting or some other way to do what i am trying to do?
Any help is appreciated.
La solution
You can use SPQuery in Powershell
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$web = Get-SPWeb http://myweb/
$list = $web.Lists["MyList"]
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.Query = $list.Views["ViewName"].Query
$items = $list.GetItems($spQuery)
$cntItems = 0
foreach($item in $items)
{
$cntItems++
}
write-host $cntItems
Autres conseils
Try the below code:
SPSite site = new SPSite("http://simosi.com");
SPWeb web = site.RootWeb;
SPList lst = web.Lists.TryGetList("Tasks");
if (lst != null)
{
SPQuery q = new SPQuery();
q.Query = lst.Views["ActiveTask"].Query;
SPListItemCollection items =lst.GetItems(q);
Console.WriteLine(items.Count);
}
Sorurce, you can also try to disable the paging as mentioned in the link.
Try below code : I think it should be better in performance wise
$web = Get-SPWeb http://portal.sharepoint.com
$list = $web.Lists["LargeList"]
$view = $list["your view name"]
$spQuery = New-Object Microsoft.SharePoint.SPQuery($view)
do
{
$listItems = $list.GetItems($spQuery)
$spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
foreach($item in $listItems)
{
Write-Host $item.Title
}
}
while ($spQuery.ListItemCollectionPosition -ne $null)
How it will help.