0

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

  • Is above code working? – Viraj Gorajia Jan 17 '17 at 7:16
  • It's not. But It was closer. Actually I replaced the part PAGE_DISPLAYFORM by PAGE_DEFAULTVIEW from the ilist2.PageType and it's working now but I dont know how to do it without removing the current display form before inserting the new view. – ameliapond Jan 17 '17 at 9:22
2

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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