I have created the SharePoint list. I have tried to add the list into page zone using PowerShell script. If I execute the script it will run without any error. But in page the list does not added. After execute more then 3 times continuously, it will get reflected in the page.
My code is :
$site = new-object Microsoft.SharePoint.SPSite($SiteUrl)
$web=$site.OpenWeb()
try
{
$page = $web.GetFile("Pages/default.aspx")
$page.CheckOut()
$webpart = New-Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart
$list=$web.Lists.TryGetList($listName)
$webpart.ListId = $list.ID
$webpartmanager = $web.GetLimitedWebPartManager($pageurl,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webpartmanager.AddWebPart($webpart,$webpartzone,$zoneorder)
$web.update()
$page.CheckIn("Added ")
$page.Publish("Added ")
$web.Close()
$web.update()
}
catch [Exception]
{
Write-Host $_.Exception.Message
}
Please suggest any idea to add list into page.