0

I am having similar issue as mentioned in below URL's.

Paging in XsltListViewWebPart is not working perfectly when added programmatically

Why don't folders work when adding an XsltListViewWebPart to the page via code?

However when I try to use SaveChanges $webpartmanager.SaveChanges($lvwp), am getting '

Object Reference not set to an instance of Object

$web = Get-SPWeb $siteUrl
$publishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
# Check if the page already exists
$currentPage = $publishingWeb.GetPublishingPages() | Where { $_.Url -eq $pageUrl}

CheckOutPage -spFile $currentPage.ListItem.File -allowUndoCheckout $false

$webPartProperty_Visible = $true  
$web = get-spweb $siteUrl  
$defaultPage = $web.GetFile($pageUrl)  

# Get the LimitedWebPartManager  
$webpartmanager=$defaultPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)  
 #Deleting existing web part
foreach ($webpart in ($webpartmanager.WebParts | Where-Object {$_.Title -eq $viewName}))
{
    write-host $siteUrl +": Existing Web part - " + $webpart.Title + " : " + $webpart.ID        
    $webpartmanager.DeleteWebPart($webpartmanager.WebParts[$webpart.ID])
    break;
}
#Create fancy GUID  
$lvwpGuid1 = [System.Guid]::NewGuid().ToString()  
$lvwpKey = "g_" + $lvwpGuid1.Replace("-","_")  

# Instantiate wp  
$lvwp = New-Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart   
$list = $web.Lists[$listName]
$lvwp.ListId = $list.ID
#$lvwp.ViewGuid =  $list.Views[$viewName].ID.ToString(); 
$lvwp.Toolbar = "Summary"
$lvwp.ID = $lvwpKey  
$code = "$" 
$lvwp.Title = $viewName   
$lvwp.Visible = $webPartProperty_Visible   
$lvwp.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::TitleOnly; 
$lvwp.HorizontalAlign = "Center" 
$lvwp.JSLink = $jsLink
# Set the view  
$lvwp.ViewGuid = $list.Views[$viewName].ID.ToString();  

# Add the web part  
$webpartmanager.AddWebPart($lvwp, $addContentTo, $positionOnPage);  
Write-Host $lvwp
$webpartmanager.SaveChanges($lvwp)

CheckInPage -spFile $currentPage.ListItem.File

$web.Update();  
$web.Dispose()

Only difference I see is URL's are using Server Side Object Model and I am trying same thing in Powershell.

Please advice.

0

Got a hint from this How to add a listviewwebpart by code

AddWebPart doesn't require SaveChanges call, so I called another function after adding all the webparts to SaveChanges and it helped.

function SaveChanges($siteUrl, $pageUrl,$viewName)
{
$web = Get-SPWeb $siteUrl
$publishingWeb =   
 [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
# Check if the page already exists
$currentPage = $publishingWeb.GetPublishingPages() | Where { $_.Url -eq $pageUrl}

CheckOutPage -spFile $currentPage.ListItem.File -allowUndoCheckout $false

$webPartProperty_Visible = $true  
$web = get-spweb $siteUrl  
$defaultPage = $web.GetFile($pageUrl)  

# Get the LimitedWebPartManager  
$webpartmanager = $defaultPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)  
 #Deleting existing web part
foreach ($webpart in ($webpartmanager.WebParts | Where-Object {$_.Title -eq $viewName}))
{
    write-host $siteUrl +"Saving WebPart" + $webpart.Title + " : " + $webpart.ID        
    $webpartmanager.SaveChanges($webPart)
    break;
}
CheckInPage -spFile $currentPage.ListItem.File

$web.Update(); 

$web.Dispose()
}

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.