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