3

I am writing a powershell script to list out all the pages in a given Pages library. But I would like to get only those pages with a given pagelayout. Is it possible to get this information from the Page library item?

The way I am currently getting the in items is using the SPFolder.Files way. Please advise. Thank you.

4

I guess this will do the trick.. But I haven't tested it.

$site = get-SPSite http://mysite.com
$web = $site.RootWeb
$pweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

$pPages = $pWeb.GetPublishingPages();
$pLayouts = $pweb.GetAvailablePageLayouts()
foreach ($pPage in $pPages)
{
    if ($pPage.Layout -eq $pLayouts)
    {
        write-host $pPage.Title
    }
}   
  • Thanks for the code. But this did not work out as expected. It looks like my site collection only has 2 publishing webs; there are a ton of other sites where I am looking to get the pages from. Also, we are comparing a collection with a string in the last if statement here. I will play around with this. But if you could think of anything else, please let me know. Thank you again! – Tom Jan 24 '14 at 21:21
  • 1
    oh sorry.. guess you need to find the correct layout in the layouts collection.. something like foreach($pLayout in $pLayouts) { if ($pLayout.Title -eq "Mylayout") { $findLayout == $playout } } and then compare with that instead – Burrhus Jan 24 '14 at 21:36
  • Worked for me, cheers! – Nullldata Mar 16 '17 at 10:58
0

Quick and dirty; this seems to work:

Function GetFilesWithSpecificPageLayout($Folder, $PageLayoutName)
{ 
    foreach($file in $Folder.Files)
    {   
        $usesGivenPageLayout = $false
        foreach ($blink in $file.BackwardLinks)
        {
            if($blink.Url.ToLower() -match $PageLayoutName.ToLower())
            {
                $usesGivenPageLayout = $true
                break
            }
        }
        if(-not $usesGivenPageLayout)
        {
            foreach ($flink in $file.ForwardLinks)
            {
                if($flink.Url.ToLower() -match $PageLayoutName.ToLower())
                {
                    $usesGivenPageLayout = $true
                    break
                }
            }
        }

        if($usesGivenPageLayout)
        {
            Write-Host "`t" $file.Name
        }
    }

    #Loop through all subfolders and call the function recursively
    foreach ($SubFolder in $Folder.SubFolders)
    {
        if($SubFolder.Name -ne "Forms")
        {  
            if($PageLayoutName -ne "")
            {
                Write-Host "`t" -NoNewline
            }
            GetFilesWithSpecificPageLayout($Subfolder)($PageLayoutName)
        }
    }
 }

#Get the Site collection 
$Site= Get-SPSite "http://sitecollection.url" 

#Loop throuh all Sub Sites
foreach($Web in $Site.AllWebs)
{
    Write-Host "'$($web.Title)' at $($web.URL)"
    foreach($list in $Web.Lists)
    {
        #Filter Doc Libs, Eliminate Hidden ones
        if(($List.BaseType -eq "DocumentLibrary") -and ($List.Hidden -eq $false) -and ($List.Title -eq "Pages"))
        { 
            GetFilesWithSpecificPageLayout($List.RootFolder)("PageLayoutNameHere")
        }
    }
}

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.