Powershell script to add/modify listviewwebpart summary view

Hi,

My requirement is to add a listviewwebpart to sevarala publishing pages and i am trying to do it through powershell.

I need only 'summary view' to be added as default when this web part is added. My example script is:

$SPWeb = Get-SPWeb $WebUrl
$wpPage = $SPWeb.GetFile("Pages\Default.aspx")


$wpPage.Checkout()

$wpManager = $wpPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$list = $SPWeb.Lists["Calendar"]
$wpView = New-Object "Microsoft.SharePoint.WebPartPages.ListViewWebPart"
 $wpView.ListName = $list.ID.ToString("B").ToUpper()

 $wpView.ViewType = "None" 

$wpView.ViewGuid = ""

$wpManager.AddWebPart($wpView,"RightZone",2)
$wpManager.SaveChanges($wpView)

But this code is adding default calendar view to the page but i need only summary view to be added to the page. how can id do that?

TIA.

July 2nd, 2013 3:35pm

You need to specify the ViewGuid property, and get rid of the ViewType property.  You can use something like this:

$view = $list.Views["Summary"]
$wpView.ViewGuid = $view.ID.ToString("B").ToUpper()

This was taken from this blog post: http://spblog.net/post/2011/04/26/Changing-default-view-for-ListViewWebPart-programmatically.aspx

That example is in C#, so in my example I may not have it written correctly, and view name will need to be verified.

Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2013 4:27pm

for some reason, Views["summary"] is not working with powershell.

it errors out saying "you cannot call a method on null valued expression" at this line.

July 2nd, 2013 4:49pm

Sorry try this instead:

$view = $SPWeb.GetViewFromUrl("/Lists/Calendar/Summary.aspx") 
Double check that URL, I pulled it out of my head.....
Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2013 4:59pm

Thsi url returns 404 not found error. Also this 'Summary View' is the option that we get within the xsltlistview web part properties when we try to edit the web part through UI. The same property seems tricky to set it through powershell. Any other ideas?
July 3rd, 2013 8:54am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics