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)




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]


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


Write-Output $summarylink.LinkUrl





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

Changing page item level permissions using event reciever

Changing item level permissions for pages library

In order to change item level permission as and when item added to a list or library, the best approach we follow is

  1. Writing event receiver for the list on ItemAdded
  2. Executing permission change block with elevated privileges.

But the same approach is not going to work on pages (publishing sites) library. The main reason for failing this approach is

  1. To change the permissions we have to execute permission block with elevated privileges. This block will run with system account user.
  2. When you are creating item with in the elevated privileges block, you will get item not found or index out of rang as the item is not available for other users.
  3. To make item available to other users, we can check in item with minor version with in the ItemAdded event receiver and then execute elevated block.
  4. If you are checking item with minor version and if the page template associated with any content type and there are any required fields then check in will fail.

This is not a bug or issue for publishing application. Pages which are checked in or published will be available for other users so when you are trying to identify an item in run with elevate privileges block , you are getting file not found or index out of rang.

To overcome this issue

  1. If content type associates to the page temple don’t have any required field then
    1. In the ItemAdded event receiver, check in the page with minor version
    2. Modify user permission with in the elevated block
    3. Checkout the item with the current user.
    4. If content type associated to the page template have required fields then
      1. Create ItemCheckedIn event receiver
      2. Make sure this event receiver code run only once.
      3. Modify user permission with in the elevated block.
      4. Train content authors to check-in the page with minor version after page created and updated required field.

Sample code block

if (!properties.ListItem.HasUniqueRoleAssignments){

SPSecurity.RunWithElevatedPrivileges(delegate() {

using (SPSite site = new SPSite(properties.WebUrl)){

using (SPWeb web = site.OpenWeb()){

SPListItem page = web.Lists[properties.ListId].GetItemById(properties.ListItem.ID);






October 12, 2012 Posted by | Event Reciever, SharePoint, SharePoint 2010, Technology | , , | Leave a comment