Thursday, May 14, 2015

PowerShell Script To Change Page Layout

Hi,

We were migrating our application from SharePoint 2010 to SharePoint 2013.
There we faced a necessity i.e. there were so many pages where page layouts of the pages needs to be changes.

Our teammate has come with the script that serves the purpose i.e.

In the Pages Library folder, if the page layout is pagelayout1,that would be changed to pagelayout2.

Below is the script that has been used to change the page layout
Function GetFolders($PagesFolder)
{   
  foreach($folder in $PagesFolder.SubFolders)
                {
                    if ($folder.Name -ne "Forms")
                    {
                                                if ($folder.Name -eq "General")
                                {

                          $folderName=$folder.Name
                           if ($folder.SubFolders.Count -ne 0)
                        {
                                                foreach ($firstLevelSubFolder in $folder.SubFolders)
                            {
                                                   $subFolderName = $firstLevelSubFolder.Name;
                                                  
                                                   GetFiles($firstLevelSubFolder);
                                                }
                                      }
                                         elseif($folder.SubFolders.Count -eq 0)
                                         {
                                         if ($folder.Files.Count -ne 0)
                                         {
                                             GetFiles($folder);
                                         }
                                         }
                    }
                                                }
                }
}


Function GetFiles($folder)
{
    foreach($file in $Folder.Files)
       {
              if($file.Name -ne "Forms")
              {
                    
                     $spFile = $spWeb.GetFile("siteurl"+ $file.Url)
              
              if($spFile.Properties["PublishingPageLayout"].Contains("Basic Page"))
              {
              }
              elseif($spFile.Properties["PublishingPageLayout"].Contains("_catalogs/masterpage/pagelayoutname.aspx"))      
              {
              }
              else
              {                                 
                     if($spFile.Properties["PublishingPageLayout"].Contains("_catalogs/masterpage/ pagelayoutname1.aspx, pagelayoutname1 "))
                     {
                                         
                           $spFile.Properties["PublishingPageLayout"] = "_catalogs/masterpage/ pagelayoutname2.aspx, /sites/nucleus/_catalogs/masterpage/ pagelayoutname2.aspx"
                           $spFile.Update()
                     }
              }
              }
       } 
}

$spWeb = Get-SPWeb("siteurl")
$spList=$spWeb.Lists["Pages"]
$spItems = $spList.Items

GetFolders($spList.RootFolder)

$spWeb.Dispose()