3

I have used the below code for change the web part properties.how to change the web part zone index in existing web part in the page? My code is :

 $pagePath = "Pages/default.aspx"
    $pageUrl = $SiteURL + $pagePath
    $web = Get-SPWeb $SiteURL
    $PublishingWeb=[Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
    $Page = $PublishingWeb.GetPublishingPages()

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

    foreach ($webpart in ($spWebPartManager.WebParts | Where-Object {$_.Title -eq "Sample Name"}))
    {
     $spWebPartManager.SaveChanges($webpart);  
     break;
    }
$Page.CheckIn("checkin")

Suggest any idea.

3 Answers 3

3

Use SPLimitedWebPartManager.MoveWebPart to move the webpart to new zone or index. Example:

$zoneId = "5"
$zoneIndex = "NewOrOldZone"
$spWebPartManager.MoveWebPart($webpart, zoneId, zoneIndex);
$spWebPartManager.SaveChanges($webpart);  
2

Try below code:

$pagePath = "Pages/default.aspx"
$pageUrl = $SiteURL + $pagePath
$web = Get-SPWeb $SiteURL
$PublishingWeb=[Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$Page = $PublishingWeb.GetPublishingPages()

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

$wp = $spWebPartManager.WebParts | Where-Object { $_.Title -eq "Sample Name" }
#change the zone according to that in your layout
$wp.ZoneID = "MiddleZone" 
$spWebPartManager.SaveChanges($wp)
$Page.CheckIn("checkin",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
$web.Close()
0

Please look at these blogs

http://www.kentd.com/2013/03/06/update-web-part-on-a-page-using-powershell-in-sharepoint-2010/

http://www.sites.se/2013/04/webpart-powershell-sharepoint-2013/

Hope it will give you an Idea. Please let me know if this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.