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, am getting 'Object Reference not set to an instance of Object - $webpartmanager.SaveChanges($lvwp) $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 commentaires:
Enregistrer un commentaire