Improve Your Technology

Just another blog for techology

Reading Summary Links using Powershell

Reading Summary Links from a web part using power shell

Similar to other web parts, reading summary link web parts is always little tricky. We need to set current context before we tried to access. Below is the sample script to read all summary links from each page.

$pages = $web.Lists[“Pages”];

foreach($page in $pages.Items)

{

if($page -eq $null)

{

continue;

}

if ($null -eq [System.Web.HttpContext]::Current)

{

$sw = New-Object System.IO.StringWriter

$resp = New-Object System.Web.HttpResponse $sw

$req = New-Object System.Web.HttpRequest “”, $web.Url, “”

$htc = New-Object System.Web.HttpContext $req, $resp

$htc.Items[“HttpHandlerSPWeb”] = $web  -as [Microsoft.SharePoint.SPweb]

[System.Web.HttpContext]::Current = $htc

}

$webPartCollection = $page.Web.GetWebPartCollection($page.Url,[Microsoft.SharePoint.WebPartPages.Storage]::Shared)

foreach($wp in $webPartCollection)

{

$webpart =  $wp -as [Microsoft.SharePoint.WebPartPages.WebPart]

if ($webpart.GetType().Name -eq ‘SummaryLinkWebPart’)

{

Write-Output $page.url

Write-Output “Summary Links:”

$sumWP = $webpart -as [Microsoft.SharePoint.Publishing.WebControls.SummaryLinkWebPart]

$sumWP.SummaryLinks

foreach ($summarylink in $sumWP.SummaryLinkValue.SummaryLinks)

{

Write-Output $summarylink.LinkUrl

}

}

}

}

Advertisements

October 12, 2012 Posted by | Power Shell, Reading Summary Links, SharePoint, SharePoint 2010 | , | 1 Comment