1

I have a sharepoint site that got migrated from SP 2010 to SP 2013. The webparts in the pages uses layouts like center left, center right, center etc. With SP 2013, they have changed the layout structure into zone 1,2,3,4 to support responsive design.

Now, I am writing a power-shell script to loop through all the webparts in my site collection and I need to find the layout of the webparts so that I can change them into zones with zone index.

The code I have to loop through the webparts, i have the following:

function enumerateWebParts($Url) {
  $site = new-object Microsoft.SharePoint.SPSite $Url    
  foreach($web in $site.AllWebs) {
    if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) {
      $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
      $pages = $pWeb.PagesList
      foreach ($item in $pages.Items) {
        $fileUrl = $webUrl + "/" + $item.File.Url;
        $manager=$item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
        $wps = $manager.webparts;

        #Which object should i select here?? Something like layout?
        #$wps | select-object @{Expression={$pWeb.Layout};

      }
    }           
  }
}

$Call = enumerateWebParts("http://mywebsiteURL/siteCollection")

How would I select the layout zone? If I am able to read their layout zone, then I can write series of if else question to change their layout zone (like from center-right to zone-2)

1 Answer 1

0

You could loop through all webparts (your $wps variable), and check the Zone property of each webpart (the type is: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.webpart.zone).

Then you should call the manager's MoveWebPart method (https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.webpartmanager.movewebpart(v=vs.110).aspx) to move the web part to appropriate zone.

Of course, your page layout must have the new zones defined already.

EDIT:

The part of the powershell script that you should add to your existing script (below the line: $wps = $manager.webparts;) is:

foreach ($wp in $wps) { 
#check the old zone of the web part
if ($wp.Zone -and $wp.Zone.Title == "YourOldZoneName")
{
    $manager.MoveWebPart($wp, "YourNewZoneName", $wp.ZoneIndex);        
}}
2
  • Thank you for the response but Is there a way to do this through power shell script? Commented Jan 9, 2017 at 15:17
  • Please see my updated answer with the code sample. Commented Jan 9, 2017 at 22:28

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.