Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Medes,

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,

Vadim

share|improve this answer
    
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 '12 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) –  Vadim Gremyachev Dec 9 '12 at 12:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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