Sunday 27 October 2013

Sharepoint 2010 - Edit Pagination Size for Blog Commets on Post Details Page

Good Morning Friends! :)

Hope you all are doing good!

Recently we received a request from Customer to update the pagination size of blog Comments webpart in Post.aspx page. (FYI, We only have a single blog site in our intranet) The default pagination size for Comments is 10 and the request from Customer was to make it 50.

The task even though looks simple is not that straightforward to implement. "Comments" webpart view on  post.aspx page is not editable when you edit the webpart. It just shows you the "Current View" in the dropdown list in webpart properties and doesn't provide any link below (like "edit the current view") to edit that view.

When you go to the "Comments" list then you will only find 3 views available inside list settings.
All Comments
My Comments  
By Author

The view which we want to edit is not present there.

So how to do it?

Well, there are couple of options you can try out:
1. Create altogether a new Comments listviewwebpart(or dataviewwebpart) having similar look and feel of the view we want. Only if you have sufficient TIME in hand.
2. Create a new custom view and a new XSLT for showing the desired view. But again time consuming.
3. Use OOB NoteBoard webpart which provides one browsable property for PageSize. But you won't be able to link existing comments to this webpart.
4. Go with powershell script to edit the Comments webpart view pagesize.

I decided to go with the last option (4th one) which was the quickest one. As we only had a single blog site, using powershell script was only the one time activity required on Test and Prod servers.

Powershell script which I have written performs following things:
- Take the reference of the blog site
- Get the reference of "Lists\Posts\Post.aspx" page
- Iterate through Webparts on this page
- Find the Comments webpart for which we want to edit the view
- Fetch the View ID of that webpart by using some string operations
-  Now get the reference of "/Lists/Comments"
- Iterate through all the views of Comments list
- Match the above "View ID" with the desired view we want to edit
- Change the RowLimit property of that view
- Update the view

& You are DONE

---------------------------------------------------------------------------------------------------
Powershell Script
---------------------------------------------------------------------------------------------------

Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

$webURL = "{Url for Blog Site Goes Here}"
$web = Get-SPWeb $webURL
$page = $web.GetFile("Lists\Posts\Post.aspx");

$webpartmanager = $web.GetLimitedWebPartManager($page.URL, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$viewid = ""
foreach($webpart in $webpartmanager.WebParts)
{
if($webpart.Title -eq "Comments")
{
$start = $webpart.XmlDefinition.IndexOf("View Name=");
$end = $webpart.XmlDefinition.IndexOf(" Type=");

$viewid = $webpart.XmlDefinition.Substring(($start + 12), 36)
Write-Host $viewid
#$webpartmanager.SaveChanges($webpart)
break;
}
}

$list = $web.GetList(($web.ServerRelativeUrl + "/Lists/Comments"))
foreach($view in $list.Views)
{
if($viewid -eq $view.ID.ToString())
{
#Write-Host "-------------------"
#Write-Host $view.RowLimit
#Write-Host "-------------------"
$view.RowLimit = 50
$view.Update()
#Write-Host $view.RowLimit
break;
}
}
---------------------------------------------------------------------------------------------------

Hope this helps!

Happy Learning! :)





No comments:

Post a Comment