0

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.

1

Just tested and this works, your $web.Close() might have something to do with the problems you're seeing.

$siteurl = "https://yoursite.com"
$listName = "Documents"
$pageUrl = "/pages/default.aspx"
$webpartzone = "Left"
$zoneorder = 0

try
{
    $web = Get-SPWeb -Identity $siteurl
    $page = $web.GetFile($pageUrl)    
    $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)

    $page.CheckIn("")  
    $page.Publish("")
}     
catch [Exception]
{
    Write-Host $_.Exception.Message
}
finally
{
    $web.Dispose()
}
1

No need for web.Update when you add webpart

Also, I usually use the below code which is somewhat different from the one you posted.

$site = new-object Microsoft.SharePoint.SPSite($SiteUrl)
$web=$site.OpenWeb()
try
{
    $page = $web.GetFile("Pages/default.aspx")    
    $page.CheckOut() 

    $list=$web.Lists.TryGetList($listName)

    $webPartManager = $web.GetLimitedWebPartManager($pageUrl,  [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

    $webpart = New-Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart
    $webpart.Title = $list.Title
    $webpart.ListName = ($list.ID).ToString("B").ToUpper()
    $webpart.ViewGuid = ($list.DefaultView.ID).ToString("B").ToUpper()
    $webpart.ZoneID = $webpartzone
    $webpart.ChromeType = "TitleOnly"

    $webpart.TitleUrl = $list.DefaultViewUrl
    $webpart.WebId = $list.ParentWeb.ID

    $webPartManager.AddWebPart($webpart, $webpartzone, $zoneorder)

    $page.CheckIn("Checked in page")  
    $page.Publish("Published page")     


}
catch [Exception]
{
   Write-Host $_.Exception.Message
}

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.