SharePoint: iterate all pages in site collection (PowerShell)
Posted in Bez kategorii
If you ever need to loop through all pages in a given SharePoint site collection, you might take a look at the PowerShell script below. While iterating through all aspx pages might be a rare need, I found it useful when I had to set some properties for WebParts through SPLimitedWebPartManager API. If you have some other idea how to use it, feel free to try this code 🙂
It crawls through the SharePoint site collection using it’s object model, and adds URL’s of pages to an array. With a little modification, the code might be limited to a scope of a SPWeb, or just views of a particular list.
When the iteration is done, all URL’s are stored in $urls
variable and can be used in some custom business logic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
$site = new-object Microsoft.SharePoint.SPSite("http://sharepoint-site-url/") $web=$site.OpenWeb() $subSites = $site.AllWebs; $urls = @() foreach ($web in $subSites) { # Files in the root directory of the website foreach ($file in $web.Files) { $url = $web.Url +"/"+ $file.Name; Write-Host "Adding URL to queue: " $url; $urls += $url; } # Site pages $lSitePages = $web.Lists["Site pages"]; if (!( $lSitePages -eq $null)) { foreach ($file in $lSitePages.Items) { $url = $web.Url +"/"+ $file.Url; Write-Host "Adding URL to queue: " $url; $urls += $url; } } # Pages for site lists foreach ($list in $web.Lists) { # Views on the lists foreach ($view in $list.Views) { $url = $web.Url + "/" + $view.Url; Write-Host "Adding URL to queue: " $url; $urls += $url; } # forms for: display, edit, new item $url = $site.Url +$list.DefaultEditFormUrl; Write-Host "Adding URL to queue: " $url; $urls += $url; $url = $site.Url +$list.DefaultNewFormUrl; Write-Host "Adding URL to queue: " $url; $urls += $url; $url = $site.Url +$list.DefaultViewUrl; Write-Host "Adding URL to queue: " $url; $urls += $url; } } # urls are now in array and can be iterated with e.g.: # foreach ($url in $urls) # { # Write-Host $url; # } |