Question

I would like to add a standard list view Webpart inside of another list form page. I'm on SharePoint 2010 and it is really easy to perform by using the UI. How to do the same by with PowerShell ? I've been visited several pages on internet but the procedures did not worked for me.

It will be great if I could do the same without removing the current list display form. So far I've been able to do this.

$web = get-spweb "http://www.questionstackexchange.fr/subsite";
$list = $web.lists["MyLittleList"];
$files = $list.rootfolder.files;

$form = $list.RootFolder.files | ?{$_.url -match "dispform.aspx"};
$form.delete();
$list.update();

$dispformurl = $list.RootFolder.ServerRelativeUrl + "/Dispform.aspx";
$dispform = $files.add($dispformurl, [Microsoft.SharePoint.SPTemplateFileType]::FormPage);
$wpm = $dispform.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
$lfw = new-object ([Microsoft.SharePoint.WebPartPages.ListFormwebpart]);
$lfw2 = new-object ([Microsoft.SharePoint.WebPartPages.ListViewWebPart]); 
$ilist = [Microsoft.SharePoint.WebPartPages.IListWebPart]($lfw);
$ilist2 = [Microsoft.SharePoint.WebPartPages.IListWebPart]($lfw2);
$ilist.ListId = $list.id;
$ilist2.ListId = "B4A7D0D0-221D-492C-B6E6-A5D758EDD3F2";
$ilist.PageType = [Microsoft.SharePoint.PAGETYPE]::PAGE_DISPLAYFORM;
$ilist2.PageType = [Microsoft.SharePoint.PAGETYPE]::PAGE_DISPLAYFORM;
$wpm.AddWebPart($lfw, "Main", 1) ;
$wpm.AddWebPart($lfw2, "Header", 1) ;
$list.DefaultDisplayFormUrl = $dispformurl;
$list.update();

I get the web part maintenance page.

Source

Was it helpful?

Solution

Try this

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$web = get-spweb "http://SharePointSite";
$list = $web.lists["MyList"];
$wpPage = $list.RootFolder.files | ?{$_.url -match "dispform.aspx"};

$wpm = $wpPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
$webpart = New-Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart
$webpart.ListId = "{C5B492BF-8869-4459-823D-21E43F10BAB9}"
$wpm.AddWebPart($webpart,"",0)
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top