Just another WordPress.com weblog

Archive for June, 2012

Find all ContentByQueryWebPart from both (Site & Publishing ) pages using PowerShell

#Add-PSSnapin Microsoft.SharePoint.PowerShell

function Get-AllCQWPFromPage($WebAppName)
{
  $webapp = Get-SPWebApplication $WebAppName
  foreach ($site in $webapp.Sites)
  {
  foreach ($web in $site.AllWebs)        
  {
   #$web.URL
   if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web))
    { 
     $pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
     $pages = $pubWeb.PagesList
     $pubPages = $pubWeb.GetPublishingPages()                    
    foreach ($item in $pages.Items)
    {
      $fileUrl=$Web.Url+”/Pages/”+$item.File.Name
                 $pubPage = $pubPages.get_Item($Web.Url+”/Pages/”+$item.File.Name)
     
                     $spFile = $web.GetFile($fileUrl)
     if($spFile.Exists)
     {
      $wpmanager = $spFile.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
      $wpcolleactions = $wpmanager.webparts
      foreach ($wp in $wpcolleactions)
      {                            
       $type = $wp.GetType()                           
        if ($type.FullName  -eq “Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart”)
       {                           
        Write-Host “Webpart Name : ” $wp.Title              
       }
      }
     }                 
            $web.Update()        
                                                  }      
   }
      elseif ($web.WebTemplate -eq “STS”) #TEAM Site
   {
    $pages = $web.Lists[“Site Pages”]             
     if ($pages)
    {               
     foreach ($item in $pages.Items)
     {                   
      $fileUrl = $pWeb.Url + $webUrl + “/” + $item.File.Url                                         
           $spFile = $web.GetFile($fileUrl)                    
           if($spFile.Exists)
       {
       $manager = $spFile.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
       $wpcolleactions = $manager.webparts
       foreach ($wp in $wpcolleactions)
       {                                                  
        $type = $wp.GetType()                          
        if ($type.FullName  -eq “Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart”) #-and ($wpcolleactions.Count -gt 0))
        {                           
         Write-Host “Webpart Name : ” $wp.Title
        }
       }
      }
     }
    }
       }    
  } 
   }
}

Get-AllCQWPFromPage “http://ServerName:999

Tag Cloud