I am looking to add a Promoted Links web part into a web part zone on a SharePoint page using Powershell. How can I do this ?
Thanks!
Try below code:
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$site = new-object Microsoft.SharePoint.SPSite("http://SharePointSite/subsite")
$spweb = $site.OpenWeb()
Write-Output $spweb.Title
$page = $spweb.GetFile("SitePages/Dummy.aspx")
Write-Output $page.Url
$wpm = $spweb.GetLimitedWebPartManager($page.Url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$listTemplate = [Microsoft.SharePoint.SPListTemplateType]::170
$listName = "Prompt Links"
$list=$spweb.Lists[$listName]
Write-Output "debug"
$ViewID = $list.Views["All Promoted Links"].id
$ViewURL = $list.Views["All Promoted Links"].url
Write-Output $ViewID
$ListViewWebPart = New-Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart
$ListViewWebPart.FrameState="Normal"
$ListViewWebPart.Title = "All Promoted Links"
$ListViewWebPart.ListName = ($list.ID).ToString("B").ToUpper()
$ListViewWebPart.ViewGuid = ($ViewID).ToString("B").ToUpper()
$ListViewWebPart.Direction="LeftToRight"
$ListViewWebPart.ZoneID = "wpz"
$ListViewWebPart.PartOrder = 0
$ListViewWebPart.TitleUrl = $ViewURL
$ListViewWebPart.DetailLink = $ViewURL
$wpm.AddWebPart($ListViewWebPart, "", 0)
I would recommend installing PnP-PowerShell and use command Add-PnPWebPartToWebPartPage
Add-PnPWebPartToWebPartPage -ServerRelativePageUrl "/sites/demo/sitepages/home.aspx" -XML $webpart -ZoneId "Header" -ZoneIndex 1
You will find a lot of other useful PS commands from this library. Just take a look at the documentation with list of all commands.