I use foreach in my scripting to make sure I get every web in every site in a specific webapp to, in this case, list (and eventually update) all Pages libraries in the SiteCollection. I've been adviced to take a look at SPSiteDataQuery, which doesn't take as long and use much less resources than my method. The only problem is that I can't seem to get it to work even if I follow blogs:

I get no result, too many rows, Exception calling GetSiteData with 1 argument and the like, but nothing really useful. So I wonder if its possible to list all Pages libraries in all web in all sites in a specific webapps using SPSiteDataQuery?

This is my current resource heavy script (which actually works):

# Add PS Snapin if not loaded
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} 
if ($snapin -eq $null) 
    Write-Host "Loading SharePoint Powershell Snapin"    
    Add-PSSnapin "Microsoft.SharePoint.Powershell"  -EA SilentlyContinue

#Iterate all webs and print List URL
$SPWebApp = Get-SPWebApplication "http://mywebapp/"

foreach ($SPSite in $SPWebApp.Sites)
    if ($SPSite -ne $null)
        foreach ($SPWeb in $SPSite.AllWebs)
            foreach ($list in $SPWeb.Lists)
                if ($list.Title.Contains("Pages"))
                    $ListURL = $SPWeb.url + "/" + $list.RootFolder.Url
                    Write-Host $ListURL

                if ($SPWeb -ne $null)

    if ($SPSite -ne $null)
up vote 2 down vote accepted

The following PowerShell code lists the pages in all Pages Libraries present in a given site collection. Variation of this can be used to list all pages libraries in a given web application. The main thing to notice here is that ServerTemplate of Pages Library in 850

$siteCollectionUrl = "http://sp2010"
$site =new-object Microsoft.SharePoint.SPSite($siteCollectionUrl)
$query = new-object Microsoft.SharePoint.SPSiteDataQuery
$query.Webs = "<Webs Scope='SiteCollection'>"
$query.Lists = "<Lists ServerTemplate='850' />"
$results = $site.rootweb.GetSiteData($query)
foreach($row in $results.rows)
  $listId = $row["ListId"]
  $webId = $row["WebId"]
  $childWeb = $site.OpenWeb([GUID]($webId))
  $pagesList = $childWeb.Lists[[GUID]($listId)];
  $itemUrl = $childWeb.Url + "/" + $pagesList.RootFolder.Url
  Write-Host $itemUrl 

I'm not a Sharepoint pro at all. But I give you this, if it can help you, it'll cool :)


(I cannot post more than 2 links =/. But the first above seems to be the more interesting)


And of course the Microsoft's one :


I apologize if it is useless =/

  • Thanks for trying. But it didn't work. The first link script is the exact same as my third link only on another page. I wonder who copied who? The last link is describing how to use SPSiteDstaQuery in C#, not PowerShell. The second link looks useful, and I'll try it later tonight. Don't appologize, we're both here to learn! – 4rchit3ct Jun 25 '13 at 15:18

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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