How To Get Inventory report Of Web parts added to SharePoint site page In SharePoint 2013


Hari Rama Krishnan
Technology Specialist
Published On :   27 May 2018
Visit Count
Today :  3    Total :   425



While planning for the Migration or other purpose sometimes, we need to know the web parts added to the SharePoint Pages, This inventory is not available on any of the existing migration tools handy. The below script will give a clear output of the WebpartName, PageURL, Title, URL, CreatedBy, WebpartID, WebpartType, ModifiedBy and Library or PageName

 $ver = $host | select version
 if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
 if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0))
 {
 Add-PSSnapin Microsoft.SharePoint.PowerShell
 }
 cls
 try{
 	#Specify a directory for which to log the results
 	$0 = $MyInvocation.MyCommand.Definition
 	$dp0 = [System.IO.Path]::GetDirectoryName($0) 
 	$LoggingDirectory = $dp0
 	$logFile=$("$dp0\Logs\WebParts_Site_SitePagesLayout_LogInformation.txt")
 	$ExecutionStartTime = (Get-Date -UFormat "%Y-%m-%d_%I:%M:%S %p").tostring()
 	Write "Script started at" $ExecutionStartTime >> $logFile
 	Write-Host "Script started at" $ExecutionStartTime
 	$PageLayout_Webparts_DetailsCSV =@()
 	#Create a variable based on the current date and time
 	$StartTime = (Get-Date -UFormat "%Y-%m-%d_%I-%M-%S %p").tostring()
 
 	#Dynamically generate the log file name
 	$Filename = "$LoggingDirectory\Result\WebPartDetails_In_SitePages_Pages_Library_" +$StartTime +".csv"
     
 	$webapplist = Get-SPWebApplication
 	foreach($webappUrl in $webapplist){
 		try{
 			#Specify the Web Application to be targeted
 			$WebApplicationURL = $webappUrl.Url
     
 			Write "`r`n web application URL" $WebApplicationURL "is being investigated" >> $logFile
 						
 			#Get all sites within each of the specified web application
 			$AllSites = Get-SPSite -WebApplication $WebApplicationURL -Limit All
     
 			
 			foreach($Site in $AllSites){
 				Write "`r`nInside Site'" + $Site.Url  >> $logFile
 	    		try{
 					#Return all webs in each site collection returned to the AllWebs variable
 				    $AllWebs = $Site.Allwebs
 				    
 				    #Perform some actions against each web retrieved from the current target site collection
 				    foreach($Web in $AllWebs){
 					try{
 							$SitePageLib = $Web.GetFolder("SitePages")
                             
 							foreach($file in $SitePageLib.Files){
     
 								if($file){
                                     if($file.Name.Contains(".aspx")){                                 
 									    $manager = $web.GetLimitedWebPartManager($file.Url,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)      
                                         
 									    $webCollection = $manager.WebParts            
 							            if($webCollection.Count -ne 0)            
 							            {            
 							            
 							                for($i =0;$i -lt $webCollection.Count; $i++)            
 							                {            
 							                    write ($i + 1).ToString()  " "  $webCollection[$i].GetType().Name  " "  $webCollection[$i].Title ',Web URL:' $Web.Url ',File URL:' $file.Url >> $logFile	
 									
 											    $PageLayout_Webparts_DetailsCSV+=New-Object PSObject -Property @{
 											    URL=$Web.Url;
 											    Title=$Web.Title;
 											    PageURL=$file.Url;	
                                                 PageTitle=$file.Title;
                                                 LibraryName='Site Pages';								
                                                 WebpartID=$webCollection[$i].ID;                                              
 											    WebpartName=$webCollection[$i].Title;
 											    WebpartType=$webCollection[$i].GetType().Name;
                                                 CreatedBy=$file.Author.DisplayName;
                                                 ModifiedBy=$file.ModifiedBy.DisplayName}							
 							                }            
 							             
 							            }
                                         if($manager){$manager.Dispose()}
 								    }
                                 }
 							}
 					
 							$PagesLib = $Web.GetFolder("Pages")
                             foreach($Item in $PagesLib.Files){
                                 if($Item){
                                     if($Item.Name.Contains(".aspx")){                                 
 									    $manager = $web.GetLimitedWebPartManager($Item.Url,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)      
                                         
 									    $webCollection = $manager.WebParts            
 							            if($webCollection.Count -ne 0)            
 							            {            
 							            
 							                for($i =0;$i -lt $webCollection.Count; $i++)            
 							                {            
 							                    write ($i + 1).ToString()  " "  $webCollection[$i].GetType().Name  " "  $webCollection[$i].Title ',Web URL:' $Web.Url ',File URL:' $Item.Url >> $logFile	
 									
 											    $PageLayout_Webparts_DetailsCSV+=New-Object PSObject -Property @{
 											    URL=$Web.Url;
 											    Title=$Web.Title;
 											    PageURL=$Item.Url;	
                                                 PageTitle=$Item.Title;								
                                                 LibraryName='Pages';  
                                                 WebpartID=$webCollection[$i].ID;                                              
 											    WebpartName=$webCollection[$i].Title;
 											    WebpartType=$webCollection[$i].GetType().Name;
                                                 CreatedBy=$Item.Author.DisplayName;
                                                 ModifiedBy=$Item.ModifiedBy.DisplayName}							
 							                }            
 							             
 							            }
                                         if($manager){$manager.Dispose()}
 								    }
                                 }
                             }
 
 
                             $Web.Dispose()
 						}
 						catch [Exception]{
 							Write $_.Exception|format-list -force >>$logFile
 							Write-Host -f red $_.Exception|format-list -force
 						}
 						finally{
 							if($Web){
 								$Web.Dispose()
 							}
 						}
 					}
 					$Site.Dispose()
 				}
 				catch [Exception]{
 					Write $_.Exception|format-list -force >>$logFile
 					Write-Host -f red $_.Exception|format-list -force
 				}
 				finally{
 					if($Site){
 						$Site.Dispose()
 					}
 				}
 			}
 
 		} 
 		catch [Exception]{
 			Write $_.Exception|format-list -force >>$logFile
 			Write-Host -f red $_.Exception|format-list -force
 		}
 
 	}
 	$PageLayout_Webparts_DetailsCSV | Export-CSV -path $Filename -notype
 
 }
 catch [Exception]{
 	Write $_.Exception|format-list -force >>$logFile
 	Write-Host -f red $_.Exception|format-list -force
 }
 $EndTime = (Get-Date -UFormat "%Y-%m-%d_%I:%M:%S %p").tostring()
 Write "Script stopped at" $EndTime >> $logFile
 Write-Host "Script stopped at" $EndTime
 



Result

clip_image002

Hope the handy script helps to get the inventory and save few hours of effort.

Happy Coding,

Hariramakrishnan Vasuthevan

Categories