1

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.

1

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
  • 1
    Thank you very much. This worked. It was a little slow so i ended up adding a $spQuery.ListItemCollectionPosition = $items.ListItemCollectionPosition line and a do while loop. – EFighter Jun 2 '15 at 5:42
0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.