Following is a part of my Powershell Script which creates a Discussion Board List and tries to add it to a default.aspx page. the problem is when I add default view to the code (marked as *) it generate this exception:"Exception calling "AddWebPart" with "3" argument(s): "The specified view is invalid." If I delete this line of code it works but the discussion board has a different view which contains only the title of the objects. However when I go to edit page and choose to add the list as a web part it works and has the threaded or unthreaded view which is right.

  $listName = "RIR Diskussion Forum"

  $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DiscussionBoard

  $web.Lists.Add($listName, "Riksrevisonens diskussions forum", [int]$listTemplate)

  $webPartManager = $web.GetLimitedWebPartManager($pageUrl,  [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$ListViewWebPart = New-Object Microsoft.SharePoint.WebPartPages.ListViewWebPart
$ListViewWebPart.Title = "Diskussion Forum"
$ListViewWebPart.ListName = ($list.ID).ToString("B").ToUpper()
$ListViewWebPart.ViewGuid = ($list.DefaultView.ID).ToString("B").ToUpper()  *
$ListViewWebPart.ZoneID = "Left"
$ListViewWebPart.ChromeType = "Default"

$ListViewWebPart.TitleUrl = $list.DefaultViewUrl
$ListViewWebPart.WebId = $list.ParentWeb.ID

$webPartManager.AddWebPart($ListViewWebPart, "Left", "0")

I need to add the List as a web part to the page by Powershell and make it to have same view as when you add it by GUI. thanks

1 Answer 1



first of all, in your example when Discussion Board instance is created, the line that gets instance of newly created Discussion Board is missing, so i believe you need to add it after line

$web.Lists.Add($listName, "Riksrevisonens diskussions forum", [int]$listTemplate)

like this one

$list = $web.Lists[$listName]

And the last one, when you add Discussion Board using GUI, it actually is instantiated using XsltListViewWebPart and not ListViewWebPart.
So if you change the line where web part is created with this one:

$ListViewWebPart = New-Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart

it would add Discussion Board on page as you expect it to.

Hope this helps,


  • thank you very much. Generally Is there a way to find out which type of web parts GUI adds to a page?
    – Medes
    Dec 9, 2012 at 11:33
  • sure, for example, if you open the page in Maintenance Mode. To open page in this mode just append the "?contents=1" to your page url. For more details please see here (blogs.msdn.com/b/tadd/archive/2009/03/27/contents-1.aspx) Dec 9, 2012 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.