0

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)
                {
                    $SPWeb.Dispose()
                }
            }
        }
    }

    if ($SPSite -ne $null)
    {
        $SPSite.Dispose()
    }
}

2 Answers 2

3

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 
  $childWeb.Dispose()
}
$site.Dispose()
0

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

http[dot]//www[dot]sharepointfix[dot]com/2012/06/powershell-script-to-get-site[dot]html

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

http://wethinkwebecome.blogspot.fr/2011/11/powershell-spsitedataquery-debug.html

And of course the Microsoft's one :

http://msdn.microsoft.com/fr-fr/library/microsoft.sharepoint.spsitedataquery%28v=office.12%29.aspx

I apologize if it is useless =/

1
  • 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!
    – Benny Skogberg
    Commented Jun 25, 2013 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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